AWS · मराठी आवृत्ती
4.4 Pipes, redirection आणि here-docs
Relay race मध्ये baton पुढे जातो तसा pipe मध्ये एका command चा output पुढच्या command चा input होतो. Shell डाव्या command चं stdout उजव्या command च्या stdin ला जोडतो.
| Symbol | काम | उदाहरण |
|---|---|---|
| | | Output पुढे input म्हणून | ps aux | grep nginx |
| > | File overwrite | echo "hi" > a.txt |
| >> | File च्या शेवटी append | date >> log.txt |
| < | File मधून input | mysql -u root -p mydb < backup.sql |
| 2> | Errors file मध्ये | find / -name x 2> errors.txt |
| 2>&1 | stderr ला stdout च्या destination कडे | ./script.sh > out.log 2>&1 |
| &> | Bash मध्ये output+errors | cmd &> all.log |
| /dev/null | Output टाकून द्या | cmd > /dev/null 2>&1 |
| tee | Screen आणि 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 पाहू.