[ bash ] compare 2 files and check if it is changed

date=`date '+%Y-%m-%d %H:%M:%S'`
latest_ads='ads.txt'
snapshot_ads="ads-$date.txt"
diff_ads='ads.diff.txt'

# download ads.txt page from $url
curl -L "$url" > "$snapshot_ads"

# get the diff into $diff_ads file
rm -f $diff_ads
diff -c $latest_ads "$snapshot_ads" > $diff_ads && echo 'same!' || echo 'differences!'
cat $diff_ads

# create latest_ads
rm -f $latest_ads
ln -s "$snapshot_ads" $latest_ads

# check if the diff is acceptable
line_diff_count=`wc -l < $diff_ads`
if [ $line_diff_count -ne 0 ]; then
echo 'The ads.txt page is changed'
exit 1
fi

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