Redirections (> >> 2>) का उपयोग
Redirection यह चुनने जैसा है कि एक tap से पानी कहां जाता है: sink के बजाय एक bucket (एक file) में। एक arrow पुराने पानी पर डालता है, दो arrows ऊपर add करते हैं, और एक और arrow सिर्फ error messages पकड़ता है।
In this page:
Syntax
command > file # overwrite stdout
command >> file # append stdout
command 2> file # redirect stderr
Redirections (> >> 2>)
> standard output को एक file में redirect करता है, इसे overwrite करते हुए; >> overwrite करने के बजाय append करता है। 2> standard error को standard output से अलग redirect करता है, और 2>&1 error को output की same stream में merge करता है।
Redirection logging और normal output को error messages से अलग करने के लिए essential है।
Warning:
> target file को तुरंत truncate करता है, भले ही बाद में आने वाला command चलने में fail हो।उदाहरण: Redirections (> >> 2>)
#!/bin/bash
echo "First line" > output.txt
echo "Second line" >> output.txt
cat output.txt
ls /no/such/path 2> error.txt
cat error.txt
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
>>मतलब होने पर>उपयोग करना, जो append करने के बजाय file overwrite करता है।- File में redirection से पहले
2>&1लिखना, जैसेcmd 2>&1 > out, इसलिए errors अब भी terminal पर जाती हैं। - एक command में same file पढ़ना और लिखना, जैसे
sort file > file, जो इसे पहले empty कर देता है।
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: