分享

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

 悦光阴 2021-01-11

这里讲的是多重嵌套。没用过 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./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 << ENDOFMESSAGEcat >>/etc/sysctl.conf<<EOF
net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216EOF

ENDOFMESSAGE

#这样就创建了一个含有EOF的脚本

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多