1

我有一个带有以下内容的文件 configure.sh(它创建了一个带有配置的 test.sh 文件,以便我最终可以将该 test.sh 用作主要配置任务)。但它不起作用

cat > /var/tmp/test.sh << EOF
regex='value=(.*)'
for i in $(cat /var/tmp/test.ini);
do
    if [[ $i =~ $regex ]];
    then
        echo ${BASH_REMATCH[1]} 
        #or
        curl -v ${BASH_REMATCH[1]}
    fi
done
EOF

当 configure.sh 执行时,它会使 test.sh 文件完全错误,例如 reged='value(.*)'

for i in 
original line1
original line1
original line1
original line1
do
  if [[ =~ ]];
  then 
  fi

done
EOF

EOF 块并没有完全按照我在上面设置的方式编写。你如何在 EOF 中写这样的字符串?

4

2 回答 2

3

更改<< EOF<< 'EOF'EOF保持末尾不变。以这种方式引用 EOF 将阻止 Bash 对 here-document 执行扩展。

于 2011-10-31T08:45:25.277 回答
0

这里文档man bash部分:

不对 word 执行参数扩展、命令替换、算术扩展或路径名扩展。如果 word 中的任何字符被引用,则分隔符是 word 上去除引号的结果,并且 here-document 中的行不展开。

所以写<< \EOF而不是<< EOF.

于 2011-10-31T08:45:20.720 回答