Trent Trent - 7 months ago 20
Bash Question

Compare executable output to text file using grep

I need a automated method to compare my C++ standard output to the 'expected output' test case files.

In the past I used a shell script that would execute my program by passing input, then comparing the output with grep to plain text. The problem now is that my output spans multiple lines and has much more test cases, making it much easier to compare expected output to a text file.

The test script I used in the past:
cd /
cd media/psf/Home/Developer/Project/
make

./a.out < tests/test01.txt | grep 'EXPECTED OUTPUT EXAMPLE' &> /dev/null
if [ $? == 0 ]; then
echo "ERROR CODE 1.1: OK"
else
echo "ERROR CODE 1.1: BAD"
fi
echo ""


How can I modify this to compare against a text file, rather than a literal string?

Answer

At least if you have Gnu diff (as it comes with Linux), which allows to use the special filename - to denote stdin, you could do

./a.out < tests/test01.txt | diff -- - golden_file.txt

to see the differences between the output of a.out and the contents of golden_file.txt. Of course, variations of this approach are possible. Have a look at the options available for diff, in particular --brief, --report-identical-files and --side-by-side.

Another possibility is to use cmp instead of diff.

Comments