0

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.

Community
  • 1
  • 1
Joshua Goldberg
  • 5,059
  • 2
  • 34
  • 39
  • "anonymous named pipe"... isn't that a funny name? – Joshua Goldberg Dec 21 '16 at 19:40
  • The correct term is *process substitution*. What problem do you have with quoting? The rules on quoting are no different using process substitution to anywhere else. – cdarke Dec 21 '16 at 20:02
  • 1
    I don't think you can do it. The named pipe used for process substitution is temporary, and goes away at the end of the command line where it's used. – Barmar Dec 21 '16 at 20:03
  • You can just use the process substitution as the first argument to your script, instead of a file name. That is, instead of `myScript fileArg`, use `myScript <(anothercmd arg1 fileArg arg3)`. – chepner Dec 21 '16 at 20:40
  • @chepner, I'm aiming to store either the filename or the pipe in a variable, so that the line that executes it is the same in either case. It looks like Ipor Sircer's answer does this. – Joshua Goldberg Dec 21 '16 at 23:52
  • @Barmar, Ipor's answer solves this by delaying the creation of the pipe (by single quoting) – Joshua Goldberg Dec 21 '16 at 23:53

1 Answers1

1

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
$
Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15
  • I want to call attention to the single quotes when defining `f` as a process. This works great for my case, because `eval cat $f` will also work unchanged for the simple file case like `f=~/.bashrc`. – Joshua Goldberg Dec 21 '16 at 23:56
  • There may be security considerations to think about if the filename is a commandline argument. – Joshua Goldberg Dec 21 '16 at 23:57