What are the error output values ​​for diff?

On the diff man page, I found these output values:

  0 No differences were found. 1 Differences were found. >1 An error occurred. 

Are there different output values ​​above 1 for different errors?

+44
bash shell diff exit-code
Aug 7 2018-11-11T00:
source share
3 answers

It depends on your diff . Mine (GNU diffutils 3.0) says:

An exit status of 0 means no differences were found, 1 means some differences were detected, and 2 means a problem. Normally, binary files are distinguished as errors, but this can be changed using -a or --text or -q or --brief .

+47
Aug 07 2018-11-11T00:
source share
— -

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.

+6
Aug 15 '11 at 16:47
source share

In my case, diff returned 127, looked for it, and found it in tldp.org "Exit Codes with Special Values"

127 "command not found" illegal command Possible problem with $ PATH or typo.

I used the wrong path for diff. :)

Font: tldp.org/LDP/abs/html/exitcodes.html

+3
Dec 22 '15 at 12:15
source share



All Articles