Your redirection isn't working because it is being applied to the parent shell, not the subshell that runs the pipeline.
If you want to send the stderr of a bunch of commands to /dev/null, you could do it this way - I am using $()
instead of backticks:
Z=$( { diff -Z $ref_out $exec_out | grep "[<>]" | wc -l; } 2>/dev/null )
Here, 2>/dev/null
applies to all the commands inside { }
.
There are many issues in your code. You could rewrite it in a better way:
if diff -Z "$ref_out" "$exec_out" 2>/dev/null | grep -q "[<>]"; then
echo "*** testcase: [ stdout - FAILED ]"
else
echo "*** testcase: [ stdout - PASSED ]"
fi
grep -q
is a better way to do this check and you won't need a wc -l
unless you want to know the exact number of matches
- you need to quote your variables
if
statement can include commands; you don't need to capture the output in order to use it in the if statement
You can use shellcheck to validate your shell script and see if you are making the usual mistakes that can break your code.