【发布时间】:2017-12-20 02:39:12
【问题描述】:
在shell脚本中如何让脚本读取输入文件字符串中的命令
示例 1 (script1.sh):
a="google.analytics.account.id=`read a`"
echo $a
示例 2 (script2.sh):
cat script2.sh
a=`head -1 input.txt`
echo $a
样本input.txt
google.analytics.account.id=`read a`
如果我运行script1.sh,read 命令工作正常,但是当我运行script2.sh 时,read 命令不会执行,而是作为输出的一部分打印出来。
所以我希望 script2.sh 具有与 script1.sh 相同的输出。
【问题讨论】:
-
@CharlesDuffy 的回答解决了您的问题,但我认为您打算在代码中使用它:
a="google.analytics.account.id=$(IFS= read -r id; echo "$id")"。 -
好点。在子 shell 中运行
read a永远不会改变父 shell 中变量a的值。