shell 多行重定向方法(多重嵌套)


這里講的是多重嵌套。沒用過 EOF的朋友請參考其他基礎貼

在自動化運維中,常常需要shell腳本。在自動化創建腳本時,會遇到腳本內容里有用EOF重定向到配置文件的代碼。

這樣就不能用EOF來創建腳本了,所以要多方法混用來實現自動化。

重定向方法1:

cat > /tmp/123.txt << EOF
        This is line 1 of the message.
        This is line 2 of the message.
        This is line 3 of the message.
        This is line 4 of the message.
        This is the last line of the message.
       
EOF

重定向方法2:參考https://linux.die.net/abs-guide/here-docs.html,有更多高級用法,純英文網站,我目前雙層嵌套就夠用了。所以沒多看。

cat > /tmp/123.txt << ENDOFMESSAGE
        This is line 1 of the message.
        This is line 2 of the message.
        This is line 3 of the message.
        This is line 4 of the message.
        This is the last line of the message.
       
ENDOFMESSAGE

嵌套方法為:

cat > /server/scripts/test2.sh << ENDOFMESSAGE

cat >>/etc/sysctl.conf<<EOF
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
EOF

ENDOFMESSAGE

#這樣就創建了一個含有EOF的腳本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM