stdin, stdout, stderr और File Descriptors
In this page:
command 1> file # stdout (fd 1)
command 2> file # stderr (fd 2)
command < file # stdin (fd 0)
echo "message" >&2
The Three Standard Streams
हर process तीन open file descriptors के साथ शुरू होता है: 0 (stdin, input), 1 (stdout, normal output), और 2 (stderr, error output)। Bash आपको N> syntax उपयोग करके इनमें से किसी को भी explicitly target करने देता है।
उदाहरण: The Three Standard Streams
#!/bin/bash
echo "this goes to stdout" 1>&1
echo "this goes to stderr" 1>&2
Login to try C/C++/Java/PHP code in the editor
Sending Error Messages to stderr
अच्छी तरह से behave करने वाली scripts diagnostics और errors को >&2 के साथ stderr पर print करती हैं ताकि stdout पर normal output clean रहे और separately pipe या capture किया जा सके।
>&2 फैलाने के बजाय इस छोटे log_error helper जैसा उपयोग करें।उदाहरण: Sending Error Messages to stderr
#!/bin/bash
log_error() {
echo "ERROR: $1" >&2
}
log_error "something went wrong"
echo "normal output continues on stdout"
Login to try C/C++/Java/PHP code in the editor
Order Matters When Merging Streams
cmd > file 2>&1 पहले stdout को file पर point करता है, फिर stderr को (अब redirected) stdout की एक copy बनाता है, इसलिए दोनों file में जाते हैं।
Order को cmd 2>&1 > file में reverse करना stderr को stdout के *old* (terminal) की एक copy बनाता है stdout file में जाने से पहले।
2>&1 > file एक बहुत common bug है क्योंकि यह वह नहीं करता जो लोग expect करते हैं।उदाहरण: Order Matters When Merging Streams
#!/bin/bash
{ echo "out line"; echo "err line" >&2; } > both.log 2>&1
echo "--- both.log contents ---"
cat both.log
rm -f both.log
Login to try C/C++/Java/PHP code in the editor
Checking Which Stream a Descriptor Points To
test -t operator (या [ -t N ]) आपको बताता है कि file descriptor N एक terminal से connected है या नहीं। एक non-interactive script में यह usually नहीं होता, जो decide करने के लिए useful है कि colored output print करना है या नहीं।
उदाहरण: Checking Which Stream a Descriptor Points To
#!/bin/bash
if [ -t 1 ]; then
echo "stdout is a terminal"
else
echo "stdout is redirected or non-interactive"
fi
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि
echoसे print किए error messages automatically stderr पर जाते हैं; plainechoहमेशा stdout (fd 1) पर लिखता है जब तक redirect न किया जाए। 2>&1placement के order को confuse करना;cmd > file 2>&1औरcmd 2>&1 > fileअलग behave करते हैं क्योंकि redirections left से right apply होते हैं।- यह मान लेना कि file descriptor numbers एक script में हमेशा के लिए fixed हैं; वे किसी भी point पर closed, duplicated, और reopened हो सकते हैं।
- File descriptor 0 stdin है, 1 stdout है, 2 stderr है।
echoऔरprintfdefault रूप से stdout पर लिखते हैं; सिर्फ explicit redirection text को stderr पर भेजता है।- Command के बाद
>&2उस command के stdout को stderr में redirect करता है। - Redirections left से right process होते हैं, इसलिए इन्हें combine करते समय order मायने रखता है।
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