Shell Function: Compare Output of Two Commands
I just wrote this up for some test automation. This function gets passed two commands, runs both commands, and returns “PASS” if they match, or “FAIL” with the diff if they do not match.
compare_output()
{
out1=/tmp/output-$$.1
out2=/tmp/output-$$.2
$1 2>&1 > $out1
$2 2>&1 > $out2
diff -q >/dev/null $out1 $out2
if [ $? != 0 ]; then
echo "FAIL"
echo
echo "Details:"
diff $out1 $out2
echo
else
echo "PASS"
fi
rm $out1 $out2
}
echo -n "Test that will fail: "
compare_output 'echo pass' 'echo fail'
echo -n "Text that will pass: "
compare_output 'echo pass' 'echo pass'
Comment
Danny Howard is 100% responsible for the content on this site, except some of it is stolen. (Sorry about that.)
Intellectual property on this web site is reserved for re-use under the terms of the Creative Commons license. Derivative works may be created for non-commercial purposes, with proper attribution.
You can contact me via e-mail: dannyman@toldme.com
Most of http://dannyman.toldme.com/ is powered by WordPress.
If you're hip to RSS and whatnot, you can subscribe to this site.