我正在尝试更改下面的 Bash 函数以执行每个命令参数。但是当我运行这个脚本时,第一个回显按预期工作,但尝试附加到scratch.txt文件的第二个回显实际上并没有执行。它只是在提示中回显。
#!/bin/sh
clear
function each(){
while read line; do
for cmd in "$@"; do
cmd=${cmd//%/$line}
printf "%s\n" "$cmd"
$cmd
done
done
}
# pipe in the text file and run both commands
# on each line of the file
cat scratch.txt | each 'echo %' 'echo -e "%" >> "scratch.txt"'
exit 0
如何让 $cmd 变量作为命令执行?
我在这里找到了答案 2 的原始代码: Running multiple commands with xargs