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
Possibly-Related Posts

Comment

Leave a comment . . .

Tiny Print:

  1. For private messages, e-mail me: dannyman@toldme.com.
  2. You must provide an e-mail address.
  3. You can use a bogus e-mail address, but I like to know who you are.
  4. I will not spam you. I will not publish or share your e-mail address.
  5. First-time commenters will be held for review.
  6. You can use these HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>