Cyber Security · मराठी आवृत्ती
Pipes, redirection आणि here-documents
या page मध्ये
एका command चा output पुढच्या command ला input म्हणून दिला की अनेक छोटी कामं जोडता येतात. Pipe साठी | वापरतो.
| Symbol | अर्थ | Example |
|---|---|---|
| (pipe) |
डाव्या command चा output उजवीकडे input | ps aux | grep nginx |
> |
File मध्ये output; जुना content overwrite | echo "hi" > a.txt |
>> |
File च्या शेवटी output जोडणे | date >> log.txt |
< |
File मधून input | mysql -u root -p mydb < backup.sql |
2> |
Errors redirect करणे | find / -name x 2> errors.txt |
2>&1 |
Errors stdout च्या सध्याच्या destination ला | ./script.sh > out.log 2>&1 |
&> |
Output आणि errors एकत्र (Bash) | cmd &> all.log |
/dev/null |
Output discard होतो | cmd > /dev/null 2>&1 |
tee |
File आणि screen दोन्हीकडे लिहिणे | echo "x" | sudo tee /etc/file |
&& / || |
यशस्वी/अपयशी झाल्यावरच पुढची command | sudo nginx -t && sudo service nginx reload |
महत्त्वाचे symbols
>output file मध्ये लिहितो आणि जुनं content overwrite करतो.>>शेवटी append करतो.<file मधून input घेतो.2>stderr/errors redirect करतो.2>&1stderr ला त्या क्षणी stdout जिथे आहे तिथे पाठवतो; order महत्त्वाचा आहे.&>Bash मध्ये stdout/stderr दोन्ही redirect./dev/nullकडे दिलेला output discard होतो.teescreen आणि file दोन्हीकडे लिहितो.&&मागची command यशस्वी असेल तर पुढची;||अपयशी असेल तर पुढची.
sudo echo ऐवजी sudo tee का?
sudo echo "text" > /etc/file मध्ये echo root ने चालू शकतो, पण redirection तुमचा shell करतो. म्हणून Permission denied येऊ शकतो. Output sudo tee /etc/file ला pipe करा. Append साठी sudo tee -a.
Here-document
अनेक lines एकत्र input देण्यासाठी here-doc वापरतो. या example मध्ये <<'EOF' आणि शेवटच्या EOF मधला text file मध्ये जातो. Delimiter quoted असल्याने $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, शेवटच्या EOF line सहित paste करा. या syntax मध्ये closing EOF line च्या सुरुवातीलाच असायला हवा आणि पुढे extra text नसावा.
Practice
ls -la /etc चा output lab file मध्ये save करा. आजची date append करा. मग /tmp मध्ये sudo tee आणि here-doc वापरून दोन-line config तयार करा.