Ravindra Bagale · AWSसर्व coursesया course चे lessonsशोधाEnglish

AWS · मराठी आवृत्ती

4.4 Pipes, redirection आणि here-docs

रवींद्र बागले यांच्या course वर आधारित · सहज मराठीत explanation

या page मध्ये

Relay race मध्ये baton पुढे जातो तसा pipe मध्ये एका command चा output पुढच्या command चा input होतो. Shell डाव्या command चं stdout उजव्या command च्या stdin ला जोडतो.

Symbolकामउदाहरण
|Output पुढे input म्हणूनps aux | grep nginx
>File overwriteecho "hi" > a.txt
>>File च्या शेवटी appenddate >> log.txt
<File मधून inputmysql -u root -p mydb < backup.sql
2>Errors file मध्येfind / -name x 2> errors.txt
2>&1stderr ला stdout च्या destination कडे./script.sh > out.log 2>&1
&>Bash मध्ये output+errorscmd &> all.log
/dev/nullOutput टाकून द्याcmd > /dev/null 2>&1
teeScreen आणि file दोन्हीकडेecho "x" | sudo tee /etc/file
&& / ||Success / failure वर पुढचं कामsudo nginx -t && sudo service nginx reload

sudo echo ऐवजी sudo tee का?

sudo echo "text" > /etc/file मध्ये > हे तुमचं shell करतं, root echo नाही. म्हणून permission error येऊ शकतो. echo "text" | sudo tee /etc/file वापरा; append साठी tee -a.

Here-document

अनेक lines एका block मध्ये file ला लिहायच्या असतील तर here-doc. Quoted 'EOF' असल्याने आतलं $HOME expand होत नाही:

sudo tee /tmp/hello.txt > /dev/null <<'EOF'
Line one
Line two with $HOME not expanded because 'EOF' is quoted
EOF
cat /tmp/hello.txt

पूर्ण block final EOF सह paste करा. शेवटचा EOF line च्या सुरुवातीला आणि त्यानंतर काहीही नसावा.

Lab

ps aux | grep sshd. मग date >> ~/labs/log.txt तीनदा करून cat ने पाहा. ls /nahi-asa-folder > out.txt 2>&1 केल्यावर error file मध्ये गेला का तपासा.

| पुढे पाठवतो, > overwrite, >> append आणि 2>&1 errors त्याच output destination कडे. पुढे backups पाहू.