0

Using Ubuntu 20.04.4 LTS (Focal Fossa) diff (GNU diffutils) 3.7

From here we have:

diff a b && echo "no difference" || echo "differences!" ;

If diff a b has exit code of 0 then said differently we have a match (all ok, Success, same inputs, no difference between a b).

How to capture the result of diff with text "no difference" and use it later in the script? How to reuse result "no difference"?

Reuse result, like a screen message:

The result from diff a b = "no difference"

Or

The result from diff a b = "Same inputs. Match. All ok. No difference. Success. "

Pablo Bianchi
  • 17,371
joseph22
  • 101

2 Answers2

0

Short answer

If you want a quick confirmation, that the files match, use the option -s,

diff -s file1 file2

Longer answer

I assume that you want to learn how to create a shellscript, so I suggest that you start with the following one,

#!/bin/bash

if ! test -f "$1" || ! test -f "$2" then echo " Usage $0 <file1> <file2>

to check if <file2> and <file2> differ and do some more operations" exit 1 fi

diff "$1" "$2"

if [ $? -eq 0 ] # testing exit status ( 0 <--> match ) then match=true else match=false fi

maybe several other commands here ...

if $match then echo "Same inputs. Match. All ok. No difference. Success. " else echo "The files are different" fi

Later on you can start modifying it (making it simpler or more complicated)

sudodus
  • 47,684
0

Using answer provided by sudodus ... with minor changes.

Both scripts work, #1 by sudodus above and #2 below.

In script #2 below, are Lines ending with ;
Is the ; to be deleted?

background
src1 is Source directory = /media/u3/usb2_drive
dest is Destination directory = /media/u3/d_driveHDD/test
script below is in part 26 thus match26

if [ $? -eq 0 ] ; # testing exit status ( 0 <--> match ) No differences echo $? = exit code = 0 ;  
then  
 match26=true ;  
else  
 match26=false ;  
fi ;

if $match26 ;
then
tput setaf 12 ; echo "diff -rq S D =" |tr '\n' ' ' ;
tput sgr0 ; echo "Match. Same inputs. All ok. No difference. Success. " ;
else
echo "Differences were found. ________________________________________ dirs differ" ;
fi ;```

joseph22
  • 101