Skip to content

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:


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)"
  echo "They are different"

if diff subject.txt differs.txt > /dev/null; then
  echo "They are the same"
  echo "They are different (expected)"

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'"
  echo "Not Found 'text to find'"
if echo "$mydiff" | grep '^> text which differs$'; then
  echo "Found 'text which differs'"
  echo "Not Found 'text which differs'"



TheSoftwareProgrammer View All

I like science and writing software.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: