在shell中,使用Here Document方式将文本重定向到文件,格式如下:
(
cat << EOF
要写的文本
EOF
) > 目标文件
示例test.sh:
#! /bin/sh
(
cat << EOF
{
"warehouse":"$1",
"ipaddr": "$2",
"bindaddr": "$2:9995",
"repopath": "./repo/",
"kafka": "100.69.168.38:9095",
"topic": "AgentHeartBeat",
"partition": "1"
}
EOF
) > agent/conf/agent.conf.json1
执行"test.sh ys 192.168.1.80"的结果:
[root@lvs00 monitor-agent]# cat agent/conf/agent.conf.json1
{
"warehouse":"ys",
"ipaddr": "192.168.1.80",
"bindaddr": "192.168.1.80:9995",
"repopath": "./repo/",
"kafka": "192.168.1.2:9095",

本文介绍了如何在Shell脚本中利用Here Document结构将多行文本内容重定向并写入到文件中,这对于批量处理和配置文件自动化非常有用。
最低0.47元/天 解锁文章
817

被折叠的 条评论
为什么被折叠?



