Substituting a command into the parameter list of another command in bash

79 views Asked by At

I have a bash script that normally works like this:

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

but I'd like to add an option to the script that would tell it to get the data from a command using an anonymous named pipe instead of the file. I.e., I'd like to do something approximating

file=<(anothercmd arg1 $1 arg3)

and have my

docommand $x $y $file $z

expand to

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

Is there a way to get the quoting right to accomplish that?

For more concrete context, the script looks at output products from regression tests, normally diffing them from a file with the expected output. I'd like to optionally pass in a revision and diff to the then-expected result, so diff $from $to would expand to diff <(hg cat -r $fromrev $from) $to.

1

There are 1 answers

2
Ipor Sircer On BEST ANSWER

Use eval:

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

example

$ 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
$