Cannot use diff and cannot use cmp.
We are able to use comm with success, but I am not getting the correct results when used with a conditional in script.
#!/bin/bash
# http://stackoverflow.com/a/14500821/175063
comm -23 <(sort /home/folder/old.txt) <(sort /home/folder/new.txt)
if [ $? -eq 0 ];then
echo "There are no changes in the files"
else
echo "New files were found. Return code was $?"
fi
It always returns:
There are no changes in the files
as the comm command, runs successfully but the contents of the files are different.
I am very limited as to what can be added to this server as it is a corporate LINUX box.
You should be able to use:
! comm -3 <(sort /home/folder/old.txt) <(sort /home/folder/new.txt) | grep -q '.*'
The comm
command will succeed (exit with status 0) whether it finds any differences or not, but grep
only succeeds if it finds a match. -q
prevents grep from printing the match(es), and the pattern '.*'
matches anything. Thus, grep -q '.?'
succeeds if its input is nonempty. But you wanted success if there was a match, so I added the !
at the beginning to invert the status.
I also made one other change: comm -23
will print lines that're in the first file (old.txt) and not in the second one (new.txt), but it will not print lines that're in the second but not the first. comm -3
will print all lines that're unique to one of the files, so it'll find lines that've been removed or added between the two files.
BTW, testing whether $?
is zero is unnecessary; just use the command directly as the if
condition:
if ! comm -3 <(sort /home/folder/old.txt) <(sort /home/folder/new.txt) | grep -q '.*'; then
echo "There are no changes in the files"
else
echo "New files were found. Return code was $?"
fi