0

Using the answer found in How can you diff two pipelines in Bash? I have written some shell scripts that I want to compare the output of:

diff <(script1 | script2) <(script3 | script4)

However, any errors printed to STDERR in any of the scripts in the subshell pipelines disappear. How can I get them to print in my outer level script (that contains the diff)?

Community
  • 1
  • 1
Rob
  • 341
  • 1
  • 3
  • 11
  • 2
    The error messages from scripts 1..4 shouldn't vanish down a black hole; you only redirected their standard output as the files given to `diff`. – Jonathan Leffler Dec 21 '15 at 22:26
  • 2
    The code you gave here won't redirect stderr anywhere, such that it'll go to console. Please test that you're providing enough information to reproduce any bug you ask about. – Charles Duffy Dec 21 '15 at 22:28
  • Well, that's kind of weird: I can't seem to reproduce it now. One of the scripts must have been silently failing. I guess I'll mark this as solved. – Rob Dec 22 '15 at 01:47

1 Answers1

2

The error messages from scripts 1..4 shouldn't vanish down a black hole; you only redirected their standard output as the files given to diff.

For example, given these files:

$ cat script1
#!/bin/bash
echo $0 stdout
echo $0 stderr >&2
$ cat script2
#!/bin/bash
echo $0 stdout
cat -
echo $0 stderr >&2
$ cat script3
#!/bin/bash
echo $0 stdout
echo $0 stderr >&2
$ cat script4
#!/bin/bash
echo $0 stdout
cat -
echo $0 stderr >&2
$

The output from your command line is:

$ diff <(script1 | script2) <(script3 | script4)
./script1 stderr
./script3 stderr
./script2 stderr
./script4 stderr
1,2c1,2
< ./script2 stdout
< ./script1 stdout
---
> ./script4 stdout
> ./script3 stdout
$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I can't seem to reproduce the failure I had: one of my scripts must have been silently failing the other day when I was troubleshooting and I mistakenly thought that STDERR was not being passed through when I passed an incorrect command line option. I was worried that I would encounter this again as I am looking to do some updates to the scripts: this gives me more peace of mind that it won't fail silently (although I'll have to revisit some of the error checking). – Rob Dec 22 '15 at 01:53