我有一个函数read_command定义为:在函数击变量替换
function read_command {
local __newline __lines __input __newstr __tmp i;
exec 3< "$*";
__newline=$'\n';
__lines=();
while IFS= read <&3 -r __line && [ "$__line" != '####' ]; do
echo "$__line";
__lines+=("$__line");
done
while IFS= read <&3 -r __line && [ "$__line" != '####' ]; do
read -e -p "${__line#*:}$PS2" __input;
local ${__line%%:*}="$__input";
done
__command="";
for i in "${__lines[@]}"; do
__tmp=$(echo "${i}");
__command="${__command} ${__newline} ${__tmp}";
done
echo -e "$__command";
}
在当前目录中有一个名为“测试”文件,用下面的 内容:
greet ${a:-"Bob"}
greet ${b:-"Jim"}
####
a: name of friend a
b: name of friend b
####
在终端中,执行的命令是
read_command test
没有输入,我期待输出最后陈述的认沽是:
greet Bob
greet Jim
但我得到的是:
greet ${a:-"Bob"}
greet ${b:-"Jim"}
这里有什么问题?
编辑:根据David的建议,在除了下面的一些情况下添加eval作品。
j=1;i="export DISPLAY=:0 && Xephyr :${j}&";k=$(eval echo "$i");
echo $k
出口DISPLAY =:0
我期待k设定 “出口DISPLAY =:0 & & Xephyr:1 &”,这里有什么错? 编辑:我试着用以下
k=$(eval "echo \"$i\"")
这是链接到我工作的脚本。 https://gist.github.com/QiangF/565102ba3b6123942b9bf6b897c05f87
行结束处的分号是多余的;在shell脚本中,作为命令分隔符,新行或分号就足够了。 – tripleee
我只是一个例子,网络上有太多不好的例子。 – godblessfq