dannyman.toldme.com


Technical

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'

Read More

Next:
Previous:
Categories: Technical

Discover more from dannyman.toldme.com

Subscribe now to keep reading and get access to the full archive.

Continue reading