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 expected 123 & 12 diff 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

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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