【发布时间】:2016-04-14 18:57:04
【问题描述】:
我想将一组字符串替换为一个新文件。源文件 file1.json 的值必须由 file1.config 中的值替换为文件 file2.json。我有以下脚本没有这样做。
file1.json
{
"colorsArray":[{
"red":"$ALERT_LEVEL",
"green":"$NORMAL_LEVEL",
"blue":"$RELAX_LEVEL"
}
]
}
file1.config
[root@ip10]# cat file1.config
ALERT_LEVEL=DANGER
NORMAL_LEVEL=NORMAL
RELAX_LEVEL=RELAX
run.sh
#!/bin/bash
set -x
if [ $# -ne 3 ]; then
echo "USAGE:
./$0 file1.json file2.json file1.config"
exit 1
fi
echo "#!/bin/bash
cat > $2 << EOF
`cat $1`
EOF" > $2;
chmod +x $2;
# load variables into env
. $3
# actually replace variables
./$2
错误:
[root@ip10]# ./run2.sh file1.json file2.json file1.config
./file2.json: line 11: warning: here-document at line 2 delimited by end-of-file (wanted `EOF')
file2.json 出现,但没有替换任何值。
root@ip10]# cat file2.json
{
"colorsArray":[{
"red":"",
"green":"",
"blue":""
}
]
}
【问题讨论】:
-
您的此处文档已损坏。如果您想将某事重定向到文件,请检查它需要像
cat << EOF > $2这样的手册页。cat >只会挂起。你可能需要逃离shebang的!。 -
马克 - 它没有帮助
-
导出变量,否则它们没有在子进程中定义
-
嗨 - 我已经导出了变量。 .代码中的 $3。
标签: json linux bash shell heredoc