How to test if files differ in BASH and test that those differences are expected
A script to test if files are different, and to further test if the differences in those files are the expected differences:
#!/bin/bash echo "text to find" | tee subject.log echo "text to find" | tee same.log echo "text which differs" | tee differs.log if diff subject.log same.log; then echo "They are the same (expected)" else echo "They are different" fi if diff subject.txt differs.txt > /dev/null; then echo "They are the same" else echo "They are different (expected)" fi echo "Capturing diff output..." mydiff=`diff subject.txt differs.txt` echo "Printing test output:" echo "$mydiff" echo "Test that the expected difference exists:" if echo "$mydiff" | grep '^< text to find$'; then echo "Found 'text to find'" else echo "Not Found 'text to find'" fi if echo "$mydiff" | grep '^> text which differs$'; then echo "Found 'text which differs'" else echo "Not Found 'text which differs'" fi
Categories