我正在尝试更改下面的bash函数来执行每个命令参数。但是,当我运行此脚本时,第一个echo按预期工作,但尝试附加到scratch.txt文件的第二个echo实际上并不执行。它只是在提示符中回显。
#!/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的原始代码: xargs with multiple commands as argument