这里讲的是多重嵌套。没用过 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的脚本