1. 程式人生 > >shell將多行文字重定向到檔案

shell將多行文字重定向到檔案

在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"的結果:

[[email protected] 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",
        "topic": "AgentHeartBeat",
        "partition": "1"
}
[
[email protected]
monitor-agent]#