There may possibly be different error codes depending on the version of diff you are using. If I remember correctly, the standard BSD diff always returned an exit code from 0, 1, or 2.
However, manpage does not display everything diff can do, but documentation that you can use to use the diff command. In a shell script, I want to know if the files match (exit = 0) or don't match (exit = 1). However, in my shell script, I also want to know that the diff command itself did not work.
diff $file1 file2 > /dev/null 2>&1 error=$? if [ $error -eq 0 ] then echo "$file1 and $file2 are the same file" elif [ $error -eq 1 ] echo "$file1 and $file2 differ" else echo "There was something wrong with the diff command" fi
Imagine if I was told that 2 means the diff command failed, but a newer version of the diff command made a distinction between a file that you cannot read (exit = 2) and a missing file (exit = 3). Now imagine if I did the following in an earlier version of the diff command, but $file2 did not exist:
diff $file1 file2 > /dev/null 2>&1 error=$? if [ $error -eq 2 ] then echo "There was something wrong with the diff command" elif [ $error -eq 1 ] echo "$file1 and $file2 differ" else echo "$file1 and $file2 are the same file" fi
In the above code, I checked the error code 2 and 1, but not 3. So, instead of detecting a missing file, I assume the files match.
The man page is trying to make sure that future OS updates will not cause most shell scripts to suddenly crash. This explains why there are separate awk and nawk and a separate grep and egrep grep .
* Updated as a comment from @chus.
David W. Aug 15 '11 at 16:47 2011-08-15 16:47
source share