- standard error (stderr) और standard output (stdout) को एक ही stream में मिलाने के लिए इस्तेमाल किया जाने वाला redirection syntax
- संख्या 1 stdout को और 2 stderr को दर्शाती है, और
& का उपयोग file descriptor को refer करने के संकेत के रूप में होता है
2>&1 का मतलब है “stderr को वहाँ भेजो जहाँ stdout इस समय जा रहा है”, और output order के अनुसार नतीजा बदल सकता है
- उदाहरण के लिए
command >file 2>&1 दोनों streams को file में भेजता है, लेकिन command 2>&1 >file में सिर्फ stderr console पर रहता है
- Bash और POSIX shell में output merge, log save, pipe processing के लिए अक्सर इस्तेमाल होने वाला अहम redirection syntax
फ़ाइल डिस्क्रिप्टर और बुनियादी अवधारणाएँ
- 0, 1, 2 क्रमशः stdin, stdout, stderr को दर्शाते हैं
/usr/include/unistd.h में defined हैं
#define STDIN_FILENO 0, #define STDOUT_FILENO 1, #define STDERR_FILENO 2
> output redirection है, `` file को नए सिरे से लिखना, और >> file में append करना है
& symbol यह बताता है कि file name नहीं बल्कि descriptor को refer किया जा रहा है
- इसलिए
2>1 का मतलब file name “1” वाली file में redirect करना है, जबकि 2>&1 का मतलब stderr को stdout पर duplicate करना है
2>&1 कैसे काम करता है
2> का मतलब stderr को redirect करना है, और &1 stdout के file descriptor को refer करता है
- नतीजतन stderr, stdout की ही destination पर जाता है
- उदाहरण:
ls -ld /tmp /tnt >/dev/null 2>&1 → दोनों output /dev/null में discard हो जाते हैं
ls -ld /tmp /tnt 2>&1 >/dev/null → सिर्फ stderr console पर रहता है
- redirection बाएँ से दाएँ process होती है, इसलिए क्रम बदलने पर नतीजा भी बदलता है
redirection order का महत्व
command >file 2>&1
- पहले stdout को file में भेजा जाता है, फिर stderr को stdout पर duplicate किया जाता है → दोनों streams file में जाती हैं
command 2>&1 >file
- पहले stderr को मौजूदा stdout (console) पर duplicate किया जाता है, फिर सिर्फ stdout को file में भेजा जाता है → stderr अब भी console पर दिखता है
- Bash redirection को क्रमवार process करता है, इसलिए command लिखते समय order पर ध्यान देना ज़रूरी है
redirection के विभिन्न उदाहरण
echo test >file.txt → stdout file में
echo test 2>file.txt → stderr file में
echo test 1>&2 → stdout को stderr पर
command &>file या command >&file → stdout और stderr दोनों file में (Bash shorthand)
command 2>&1 | tee -a file.txt → दोनों streams को file और terminal पर एक साथ output करना
उन्नत उपयोग और Bash 4.0 के बाद की सुविधाएँ
- Bash 4.0 से process substitution का उपयोग करके output को अलग-अलग भेजना संभव है
ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /')
- stdout और stderr को अलग-अलग filter में भेजता है
|& , 2>&1 | का shorthand है, जो दोनों streams को मिलाकर pipe में भेजता है
set -o noclobber option मौजूदा file को overwrite होने से रोकता है, और >| से exception दिया जा सकता है
व्यावहारिक उपयोग के उदाहरण
g++ main.cpp 2>&1 | head → compile errors समेत शुरुआती output ही देखना
perl test.pl > debug.log 2>&1 → सभी output और errors को log file में save करना
foo 2>&1 | grep ERROR → stdout और stderr दोनों में “ERROR” string ढूँढना
docker logs container 2>&1 | grep "some log" → पूरे logs को pipe के ज़रिए भेजना
मुख्य सार
2>&1 , stderr को stdout पर duplicate करने वाला POSIX standard syntax है
- redirection order ही नतीजा तय करता है, इसलिए command लिखते समय सावधानी ज़रूरी है
- Bash में
&> से दोनों streams को एक साथ handle किया जा सकता है,
log management, pipe processing, error merge जैसी कई automation scripts में इसका अनिवार्य रूप से उपयोग होता है
अभी कोई टिप्पणी नहीं है.