Discussion:
[Help-bash] How to make sure >() is finished when a command line is finished?
Peng Yu
2018-04-22 02:59:58 UTC
Permalink
In the following example, 'o: stdout' is printed after the second
command line is called. Is there a way to make sure it is finished
when the first command line is finished? Thanks.

$ ./main.sh
./script.sh > >(awk -v oprefix='o: ' '{print oprefix $0; fflush(); }')
awk -v oprefix='o: ' '{print oprefix $0; fflush(); }'
stderr
./script.sh 2> >(awk -v eprefix='e: ' '{print eprefix $0 >>
"/dev/stderr"; fflush(); }')
awk -v eprefix='e: ' '{print eprefix $0 >> "/dev/stderr"; fflush(); }'
o: stdout
stdout
e: stderr

$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
./script.sh > >(awk -v oprefix='o: ' '{print oprefix $0; fflush(); }')
./script.sh 2> >(awk -v eprefix='e: ' '{print eprefix $0 >>
"/dev/stderr"; fflush(); }')
--
Regards,
Peng
Russell Lewis
2018-04-22 12:47:21 UTC
Permalink
My (hackish) solution is to pipe the first line of your script into cat:
cmd1 > >(cmd2) | cat

Since cat won't terminate until its stdin closes, and cmd2's stdout is
hooked to that, cat doesn't terminate until cmd2 does.

Of course, this doesn't work if you redirect cmd2's stdout.

Russ
Post by Peng Yu
In the following example, 'o: stdout' is printed after the second
command line is called. Is there a way to make sure it is finished
when the first command line is finished? Thanks.
$ ./main.sh
./script.sh > >(awk -v oprefix='o: ' '{print oprefix $0; fflush(); }')
awk -v oprefix='o: ' '{print oprefix $0; fflush(); }'
stderr
./script.sh 2> >(awk -v eprefix='e: ' '{print eprefix $0 >>
"/dev/stderr"; fflush(); }')
awk -v eprefix='e: ' '{print eprefix $0 >> "/dev/stderr"; fflush(); }'
o: stdout
stdout
e: stderr
$ cat main.sh
#!/usr/bin/env bash
set -v
./script.sh > >(awk -v oprefix='o: ' '{print oprefix $0; fflush(); }')
./script.sh 2> >(awk -v eprefix='e: ' '{print eprefix $0 >>
"/dev/stderr"; fflush(); }')
--
Regards,
Peng
Loading...