0
我需要有一个可变的shell命令在一个xargs的呼叫使用指定的shell命令失败。xargs的打电话的时候,用一个变量
... xargs的-I {} SH -c 命令 ...
我发现当命令是 '文字',但是当我通过一个shell变量指定失败xargs的工作。
任何建议排除此故障?
下面是示例代码。
## xargs call with literal shell command
# works; creates file abcd1234.txt containing string 'abcd1234'
echo 'abcd1234' | xargs -I {} -n 1 sh -c 'echo {} | grep "\d" > {}.txt'
## xargs call with variable as shell command
# create shell command to give to xargs
cmd1='echo'
cmd2='grep "\d"'
command=${cmd1}' {} | '${cmd2}' > {}.txt'
# returns the literal command that works: echo {} | grep "\d" > {}.txt
echo $command
# fails
echo 'abcd1234' | xargs -I {} -n 1 sh -c $(echo $command)