Last active
March 12, 2021 07:22
-
-
Save mrizvic/22578238864e0b72003b88efd26e827c to your computer and use it in GitHub Desktop.
cat file if file content has changed from last run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
help_string="cat or diff file if content has changed from last run" | |
FILE=$1 | |
ME=$(basename $0) | |
if [ -z "$FILE" ]; then | |
echo "USAGE: $ME <resultfile.txt>" | |
echo "EXAMPLE: ifdiff.sh /tmp/changing-file.txt - cat file when it was changed from previous run" | |
echo " ifdiff.sh /tmp/changing-file.txt diff - show what changed from previous run" | |
echo "exit with status 2 if there is no change on file since last run" | |
echo "" | |
exit 1 | |
fi | |
FILE=$(readlink -f $FILE) | |
if [ ! -f "$FILE" ]; then | |
exit 1 | |
fi | |
OFILE="$FILE".OLD | |
if [ ! -f "$OFILE" ]; then | |
cat $FILE | |
cp $FILE $OFILE | |
else | |
if cmp -s "$FILE" "$OFILE" ; then | |
exit 2 | |
else | |
### WHEN SECOND ARGUMENT IS PROVIDED SHOW DIFF OTHERWISE CAT | |
if [ -z $2 ]; then | |
cat $FILE | |
else | |
diff $FILE $OFILE | |
fi | |
cp $FILE $OFILE | |
exit 0 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment