linux常用命令 / shell 及相关 / 未分类 · 2012年11月20日

结合 shell heredoc 追加hosts

以前 追加老是用到echo 或者 sed -i ‘$a cmd’
今天使用了一下heredoc ,虽然最早是在学php时认识的,感觉方便 很多 ,以前 lfs的heredoc好像与下面的不太一样
here doc 的一般用法:
[cmd] <cat > file <
也可以写进shell脚本
下面是例子
第一用excle 拉出所有要填写的域名
212.app1.app.com
213.app1.app.com
********
第二 sed -i ‘s/^/s/’ file #在每行首加上s
第三 paste file1 file2 or sed -i ‘s/^/10.190.236.216 /’ file2 #写成 hosts正确的格式
第四
./comm_tool “tgw2” “cat << EOF >> /etc/hosts
10.190.236.216 s212.app1.app.com
10.190.236.216 s213.app1.app.com
EOF”
shell heredoc 说得不错的url
http://febird.iteye.com/blog/588509
http://blog.csdn.net/whinah/article/details/5294265