[solved] why does "diff" always return "1"?!? - Page 1
Forum section: electronics / general

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 01:15:05 pm
content: bash scriptCode:function check_result(){local ans="$?" # get the exit status from a terminated programlocal answif [ "$ans" == "0" ] # 0=success, 1=failurethenansw="success"elseansw="failure (ans=$ans)"fiecho "$answ"}echo "AAAA" > Acp A Becho "BBBB" >> Bdiff -Naur A Bcheck_result# -N, --new-file: treat absent files as emptydiff -Naur A C # C doesn't existcheck_resultCode:--- A 2021-09-25 13:52:21.012000000 -0000+++ B 2021-09-25 13:52:21.012000000 -0000@@ -1 +1,2 @@AAAA+BBBBfailure (ans=1)failure (ans=1)--- A 2021-09-25 13:52:21.012000000 -0000+++ C 1970-01-01 00:00:00.000000000 -0000@@ -1 +0,0 @@-AAAAfailure (ans=1)failure (ans=1)Code:NAMEGNU diff - compare files line by lineSYNOPSISdiff ... FILESDESCRIPTIONCompare FILES line by line.Mandatory arguments to long options are mandatory for short options too.--normaloutput a normal diff (the default)-q, --briefreport only when files differ-s, --report-identical-filesreport when two files are the same-c, -C NUM, --context[=NUM]output NUM (default 3) lines of copied context-u, -U NUM, --unified[=NUM]output NUM (default 3) lines of unified context-e, --edoutput an ed script-n, --rcsoutput an RCS format diff-y, --side-by-sideoutput in two columns-W, --width=NUM--left-columnoutput only the left column of common lines--suppress-common-linesdo not output common lines-p, --show-c-functionshow which C function each change is in-F, --show-function-line=REshow the most recent line matching RE--label LABELuse LABEL instead of file name and timestamp (can be repeated)-t, --expand-tabsexpand tabs to spaces in output-T, --initial-tabmake tabs line up by prepending a tab--tabsize=NUMtab stops every NUM (default 8) print columns--suppress-blank-emptysuppress space or tab before empty output lines-l, --paginatepass output through 'pr' to paginate it-r, --recursiverecursively compare any subdirectories found--no-dereferencedon't follow symbolic links-N, --new-filetreat absent files as empty--unidirectional-new-filetreat absent first files as empty--ignore-file-name-caseignore case when comparing file names--no-ignore-file-name-caseconsider case when comparing file names-x, --exclude=PATexclude files that match PAT-X, --exclude-from=FILEexclude files that match any pattern in FILE-S, --starting-file=FILEstart with FILE when comparing directories--from-file=FILE1compare FILE1 to all operands; FILE1 can be a directory--to-file=FILE2compare all operands to FILE2; FILE2 can be a directory-i, --ignore-caseignore case differences in file contents-E, --ignore-tab-expansionignore changes due to tab expansion-Z, --ignore-trailing-spaceignore white space at line end-b, --ignore-space-changeignore changes in the amount of white space-w, --ignore-all-spaceignore all white space-B, --ignore-blank-linesignore changes where lines are all blank-I, --ignore-matching-lines=REignore changes where all lines match RE-a, --texttreat all files as text--strip-trailing-crstrip trailing carriage return on input-D, --ifdef=NAMEoutput merged file with '#ifdef NAME' diffs--GTYPE-group-format=GFMTformat GTYPE input groups with GFMT--line-format=LFMTformat all input lines with LFMT--LTYPE-line-format=LFMTformat LTYPE input lines with LFMTThese format options provide fine-grained control over the outputof diff, generalizing -D/--ifdef.LTYPE is 'old', 'new', or 'unchanged'.GTYPE is LTYPE or 'changed'.GFMT (only) may contain:%< lines from FILE1%> lines from FILE2%= lines common to FILE1 and FILE2%[-][.]{doxX}LETTERprintf-style spec for LETTERLETTERs are as follows for new group, lower case for old group:F first line numberL last line numberN number of lines = L-F+1E F-1M L+1%(A=B?T:E)if A equals B then T else ELFMT (only) may contain:%L contents of line%l contents of line, excluding any trailing newline%[-][.]{doxX}nprintf-style spec for input line numberBoth GFMT and LFMT may contain:%% %%c'C' the single character C%c'\OOO'the character with octal code OOOC the character C (other characters represent themselves)-d, --minimaltry hard to find a smaller set of changes--horizon-lines=NUMkeep NUM lines of the common prefix and suffix--speed-large-filesassume large files and many scattered small changes--color[=WHEN]colorize the output; WHEN can be 'never', 'always', or 'auto' (the default)--palette=PALETTEthe colors to use when --color is active; PALETTE is a colon-separated list of terminfo capabilities--help display this help and exit-v, --versionoutput version information and exitFILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'. If --from-file or --to-file is given,there are no restrictions on FILE(s). If a FILE is '-', read standard input. Exit status is 0 if inputs arethe same, 1 if different, 2 if trouble.I am working on a bash-script, and I am confused by the exit status of the tool "diff", because it always return "1", which should mean FAILURE and others programs return "0" to say "success".So why does it always return "1" ?!?

author: magic
author_rank: Super Contributor
timestamp: September 25, 2021, 01:19:58 pm
content: Maybe read TFM that you posted above?

author: retiredfeline
author_rank: Frequent Contributor
timestamp: September 25, 2021, 01:39:03 pm
content: I haven't looked at your commands but you are doing this the complicated way.Code:if diff  A Bthenecho "No differences"elseecho "Differences"fi

author: thinkfat
author_rank: Unknown
timestamp: September 25, 2021, 01:42:34 pm
content: diff behaves exactly as described in the man page your posted above.

author: PKTKS
author_rank: Super Contributor
timestamp: September 25, 2021, 01:51:19 pm
content: not on my shells0 = match1 = mismatch2 = errorPaul

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 03:28:36 pm
content: Quote from: magic on September 25, 2021, 01:19:58 pmwhere is it written that it always return "0" as exit status?Code:int main(){.../** 0 success* 1 failure*/return exit_status;}

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 03:29:29 pm
content: Quote from: thinkfat on September 25, 2021, 01:42:34 pmthe man page doesn't mention the return status

author: Siwastaja
author_rank: Super Contributor
timestamp: September 25, 2021, 03:30:35 pm
content: Quote from: DiTBho on September 25, 2021, 03:29:29 pmYou literally quoted it! Look at the very last sentence.

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 03:33:41 pm
content: Code:echo "hAllo"check_resultls ~check_resultping 192.168.1.11 -c1check_resultCode:hAllosuccessBook-CompilerDesignInC.pdfXilinx-ISE-v14.7-1015-1-Linux.tarsuccessPING 192.168.1.11 (192.168.1.11) 56(84) bytes of data.64 bytes from 192.168.1.11: icmp_seq=1 ttl=64 time=0.720 ms--- 192.168.1.11 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 0.720/0.720/0.720/0.000 mssuccess

author: Siwastaja
author_rank: Super Contributor
timestamp: September 25, 2021, 03:37:12 pm
content: You seem to have some misunderstanding that there is some universal standard that denotes 0 = success, others = failure. This is not the case. Return values are per program, and defined in the manual. Some programs do not generate information which should be returned using return value, but only return error / success in which case it's easy to just decide 0 = success, others = failure codes. Some programs, likediff, return theresultof the program (differs; no difference),andalso have to return errors. They chose the codes as described on the manual you can't read. Some others have chosen negative codes for errors, 0 for success, and positive numbers for non-error status codes, which is quite nice. Whoever wrotediffdidn't do that, but that's OK too.

author: retiredfeline
author_rank: Frequent Contributor
timestamp: September 25, 2021, 03:37:38 pm
content: The Posix man page diff (1p) says:Code:EXIT STATUSThe following exit values shall be returned:0 No differences were found.1 Differences were found.>1 An error occurred.The GNU diff man page refers you to info diff which says the same thing.

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 03:41:01 pm
content: Quotedamn, the man-page says it in the last line, I didn't read it.This happen when you are really tired to fix bugs here and there.anyway I always get "1" from diff even when inputs are the same, and never get "0" or "2", but always "1", which is wrong.I will re-implement "diff" myself as built-in function for the application I am developing, I am tired to waste time with that ..

author: retiredfeline
author_rank: Frequent Contributor
timestamp: September 25, 2021, 03:46:18 pm
content: Quote from: DiTBho on September 25, 2021, 03:41:01 pmThere's something wrong with your script then. What do you get when you do:Code:diff /etc/issue /etc/issueecho $?

author: PKTKS
author_rank: Super Contributor
timestamp: September 25, 2021, 03:50:57 pm
content: Quote from: retiredfeline on September 25, 2021, 03:46:18 pmYes there is a very optimistic assumption that the function will magically return that code...It will not happen that way...Code:<EXECUTE DIFF HERE>err_cod=$? # grab returned error code wo quotes asapif [ ${err_cod} -eq 0 ]; then # 0=success, 1=failurethat is the propermodern bashsyntaxPaul

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 04:15:17 pm
content: Quote from: retiredfeline on September 25, 2021, 03:46:18 pmalways "1", and it makes no sense.

author: PKTKS
author_rank: Super Contributor
timestamp: September 25, 2021, 04:30:39 pm
content: Quote from: DiTBho on September 25, 2021, 04:15:17 pmThis is wrong.Copy paste a shot w/your shell bash version and diff version and this code executed.Then recompile your bash shell making dead sure your areNOTusing bash malloc.There are known issues and bugs using that optionPaul

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 04:37:27 pm
content: Quote from: PKTKS on September 25, 2021, 03:50:57 pmwell, I haveCode:# wc -l lib_patch_core.sh1001 lib_patch_core.sh1000 lines of bash code that works perfectly. It suffices to prove that it is not "bad bash syntax", but rather it must be something specifically wrong with "diff".So, I tested the same bash-script on a different Linux-box (mac-mini/intel), and this time diff returned { 0 1 } as described in the manual.So I re-emerged "sys-apps/diffutils" on the first Linux box (mac-mini/ppc) I have been working for days and upgraded the diffutils package to v3.7-r2.mac-mini/intel, sys-apps/diffutils-v3.7-r2 (diff works as described in the manual)mac-mini/ppc, sys-apps/diffutils-v2.8.7-r2 (diff always returns "1")I tried the bash script with the new /usr/bin/diff and this time diff worked as expectedComputers

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 04:43:23 pm
content: mac-mini/ppc bash v4.1mac-mini/intel bash v5.0by updating diffutils it also updated bash from v4.1 to v5.0

author: PKTKS
author_rank: Super Contributor
timestamp: September 25, 2021, 05:25:09 pm
content: Quote from: DiTBho on September 25, 2021, 04:43:23 pmlatest bash fix is 5.1.8 - ded sureWITHOUT bash_malloclatest diff is 3.8it should by all means give the proper result as belowotherwise you have a broken system wide lib setPaul

author: thinkfat
author_rank: Unknown
timestamp: September 25, 2021, 05:37:51 pm
content: Ppc always is full of headaches. In my time as head of a commercial embedded Linux distribution, 90% of the really odd bugs we found were on that architecture.

author: DiTBho
author_rank: Super Contributor
timestamp: September 25, 2021, 08:24:35 pm
content: Quote from: thinkfat on September 25, 2021, 05:37:51 pmRight, speaking of the kernel, we have a lot of quirks for Apple controllers, what's another? User space quirks and headaches couldn't really miss the party

author: thinkfat
author_rank: Unknown
timestamp: September 26, 2021, 06:17:44 pm
content: I remember some python tests failing, I think Unicode handling was somehow broken, also some floating point stuff. But that is only the tip of the iceberg

author: bson
author_rank: Unknown
timestamp: October 30, 2021, 07:30:39 am
content: Use cmp(1) if you just want to see if two files are identical.

author: DiTBho
author_rank: Super Contributor
timestamp: October 30, 2021, 10:51:23 pm
content: Quote from: bson on October 30, 2021, 07:30:39 amThanks. Diff is used in a complex Bash script that needs to check if two files are equal or not, and in case not it needs to report differences. My script allocates the output of Diff in a variable to have it ready.But /usr/bin/cmp is something I didn't have on my system. Now I have it. It's part of the diffutils-v3.7-r1

author: magic
author_rank: Super Contributor
timestamp: October 31, 2021, 06:59:33 am
content: Note that cmp has similarly "problematic" exit codes as diff

author: DiTBho
author_rank: Super Contributor
timestamp: October 31, 2021, 09:19:35 am
content: yup, but at least it's not buggy (well, ok, because - it, bash, and their friends - have been already updated)

author: bson
author_rank: Unknown
timestamp: November 01, 2021, 03:52:54 am
content: Quote from: DiTBho on October 30, 2021, 10:51:23 pmcmp is faster and if it were me I'd use a pattern like:Code:if ! cmp $FILE1 $FILE2 ; thenecho '***' $FILE1 differs from $FILE2 '***'diff $FILE1 $FILE2exit 1fiThis way, in the normal case where they don't differ you can quickly and silently move on and do nothing. Then use diff to produce the actual message to print if they do differ.