1828577 Members
2325 Online
109982 Solutions
New Discussion

Re: diff return codes

 
Ian Lochray
Respected Contributor

diff return codes

The man page for diff just says that errors return > 1. Where can I find out what specific return codes from diff mean?
5 REPLIES 5
Peter Nikitka
Honored Contributor

Re: diff return codes

Hi,

the SUN man pages tell (no HP at hand here)
0 No diffs
1 Diffs
>1 Error

Many commands use '2' for ENOENT - errors 'no such file or directory'.

For me the return values mentioned are succifient (Bourne-shell syntax):
case $? in
0) print same;;
1) print diff;;
*) pint error;;
esac

BTW: I prefer 'cmp -s' for a testing of identical file content:
if cmp -s f1 f2; then print same;fi

- 'cmp' quits after the first mismatch, which is good for large files
- In most cases I do not need a listing of the differences; cmp -s just sets return codes.

mfG Peter


The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Ian Lochray
Respected Contributor

Re: diff return codes

I would like to know the return codes because the script I am using should take different action depending on why the diff fails e.g. if one of the files doesn't exist. I realise that I could test this before or after the diff but that's just an extra step.
Steven Schweda
Honored Contributor

Re: diff return codes

> [...] Where can I find out [...]

Reliably, from the documents or the source
code.

If the documents don't satisfy, and the
source code is unavailable, then you're left
with experiment. How many different return
status values have you seen? Knowing
nothing, I have no reason to believe that
"diff" returns interesting values greater
than one.

Source for GNU "diff" _is_ readily available.

One of the many problems with relying on
experiment rather than the documents is that
whatever you discover could become obsolete
at the next "diff" update. Or when you move
to another system, where "diff" is different.
Peter Nikitka
Honored Contributor

Re: diff return codes

Hi,

as Steven told you - don't expect to much from retern values >1.
You can start a test series, giving diff non-existing or unreadable file(s) as 1st and/or 2nd argument, but I don't think you get something different as '2'.

I think, you should be able to do this by yourself:

diff file1 file2
case $? in
0) print same;;
1) print diff;;
*)
[ -e file1 ] || print -u2 missing file1
[ -d file1 ] && print -u2 a dir: file1
...
;;
esac

mfG Peter

PS: 1 point received - whow!!
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Steven Schweda
Honored Contributor

Re: diff return codes

> PS: 1 point received - whow!!

Points awards in these forums often reveal
more about the questioner than about the
quality of the responses.


> Source for GNU "diff" [...]

A quick look suggests that it uses
EXIT_SUCCESS (0), EXIT_FAILURE (1), and
EXIT_TROUBLE (2), but there could be more in
there that I didn't notice. Code like:

[...]
int status = compare_files (NULL, from_file, argv[optind]);
if (exit_status < status)
exit_status = status;
[...]

suggests that bigger values imply bigger
problems. (But, if such stuff isn't
documented, then what good is it?)