将命令替换为bash中另一个命令的参数列表

时间:2016-12-21 19:40:07

标签: bash shell parameters pipeline expansion

我有一个通常像这样工作的bash脚本:

[...]
file=$1
docommand $x $y $file $z

但我想在脚本中添加一个选项,告诉它使用anonymous named pipe而不是文件从命令中获取数据。 I.e。,我想做一些近似的事情

file=<(anothercmd arg1 $1 arg3)

并拥有我的

docommand $x $y $file $z

扩展到

 docommand $x $y <(anothercmd arg1 $1 arg3) $z

有没有办法让引用权能完成呢?

对于更具体的上下文,脚本查看回归测试的输出产品,通常从具有预期输出的文件中区分它们。我希望可以选择传递修订版并将差异转换为当时预期的结果,因此diff $from $to会扩展为diff <(hg cat -r $fromrev $from) $to

1 个答案:

答案 0 :(得分:1)

使用eval:

eval docommand $x $y <(anothercmd arg1 $1 arg3) $z

例如

$ f='<(ps)'
$ echo $f
<(ps)
$ cat $f
cat: '<(ps)': No such file or directory
$ eval cat $f
  PID TTY          TIME CMD
 4468 pts/8    00:00:00 mksh
 4510 pts/8    00:00:00 bash
 4975 pts/8    00:00:00 bash
 4976 pts/8    00:00:00 cat
 4977 pts/8    00:00:00 ps
$
相关问题