Pipes और Commands को Chain करना
In this page:
command1 | command2
command1 && command2
command1 || command2
Connecting Two Commands
Pipe operator | अपने left वाले command का stdout लेता है और इसे right वाले command के stdin के रूप में feed करता है। आप इस तरह जितने चाहें commands chain कर सकते हैं।
उदाहरण: Connecting Two Commands
#!/bin/bash
printf "banana\napple\ncherry\n" | sort
Login to try C/C++/Java/PHP code in the editor
Pipelines and Exit Status
Default रूप से, एक pipeline का exit status इसके आखिरी command का exit status होता है, चाहे पहले वाले commands fail हुए हों या नहीं। pipefail enable करना इसे बदल देता है इसलिए pipeline fail होता है अगर कोई भी stage fail हो।
pipefail के बिना, एक लंबी pipeline में एक broken पहली stage silently ignore हो सकती है।उदाहरण: Pipelines and Exit Status
#!/bin/bash
false | true
echo "default pipeline status: $?"
set -o pipefail
false | true
echo "pipefail status: $?"
set +o pipefail
Login to try C/C++/Java/PHP code in the editor
Chaining with && and ||
&& अगला command सिर्फ तब चलाता है जब पिछला succeed हुआ हो (exit status 0); || अगला command सिर्फ तब चलाता है जब पिछला fail हुआ हो (non-zero exit status)। ये lightweight conditional logic के लिए लगातार उपयोग होते हैं।
उदाहरण: Chaining with && and ||
#!/bin/bash
mkdir -p demo_dir && echo "directory ready"
rmdir demo_dir || echo "could not remove"
false || echo "this runs because the previous command failed"
true && echo "this runs because the previous command succeeded"
Login to try C/C++/Java/PHP code in the editor
Building a Small Pipeline
असली scripts अक्सर एक बड़ा command लिखने के बजाय कई छोटे, single-purpose tools chain करती हैं, जो simple programs compose करने की Unix philosophy को mirror करता है।
उदाहरण: Building a Small Pipeline
#!/bin/bash
printf "3\n1\n2\n1\n3\n1\n" | sort -n | uniq -c
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि एक pipeline commands को एक के बाद एक चलाती है; pipeline में हर command असल में एक ही समय पर शुरू होता है और उनके बीच data stream होता है।
- एक pipeline के बाद
$?check करना और यह मान लेना कि यह पहले command का exit status reflect करता है; default रूप से यह सिर्फ आखिरी command का exit status reflect करता है। - यह भूल जाना कि pipe का हर side अपनी खुद की subshell में चलता है, इसलिए एक piped command के अंदर set किए variables बाद में persist नहीं करते।
|एक command का stdout सीधे अगले command के stdin से connect करता है।- Default रूप से, एक pipeline के बाद
$?सिर्फ आखिरी command का exit status reflect करता है। set -o pipefailpipeline का exit status सभी commands में से पहला non-zero status बना देता है।- Commands को
&&(सिर्फ success पर अगला चलाएं) और||(सिर्फ failure पर अगला चलाएं) से chain किया जा सकता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null