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:

#!/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

Linux

TheSoftwareProgrammer View All

I like science and writing software.

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 )

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: