The script is test.sh
.
#!/bin/sh
if cmp -s file.a file.b; then
echo diff
else
echo same
fi
When checking the exit code from cmp -s
I see:
When file.a
and file.b
are different.
cmp -s file.a file.b
echo $?
1
When file.a
and file.b
are the same.
cmp -s file.a file.b
echo $?
0
Why then does the conditional statement seem to operate in the reverse?
When file.a
and file.b
are different.
./test.sh
same
When file.a
and file.b
are the same.
./test.sh
diff
This isn't the first time I've seen this. I must be misunderstanding how the if
statement interprets the exit code, or maybe it isn't the exit code at all?
if
-- https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_07 – glenn jackman Jan 07 '22 at 15:38