Verken
Verken

Reputation: 45

bash - difference between two text files

Let's say there are two text files and I need to check if they are different. If they are, I need to make some changes to them and display information on the terminal.

Will something like this work?

diff file1.txt file2.txt > difference.txt

if [ -s difference.txt ]
then
    .....
else
    .....
fi

I also tried to find some other ways of writing this in bash, and I've found this code :

  DIFF_OUTPUT="$(diff new.html old.html)"
  if [ "0" != "${#DIFF_OUTPUT}" ]; then

But I can't quite understand it. I guess in the first line we create a variable DIFF_OUTPUT which works just like difference.txt in my code? Then there's ${#DIFF_OUTPUT} which I don't understand at all. What's going on here?

I apologise if my questions are very basic, but I couldn't find an answer anywhere else.

Upvotes: 3

Views: 1204

Answers (3)

chepner
chepner

Reputation: 531908

diff has an exit status of 1 if the files are different.

diff file1.txt file2.txt > difference.txt
status=$?

case $status in
    0) echo "Files are the same"
       # more code here
       ;;
    1) echo "Files are different"
       # more code here
       ;;
    *) echo "Error occurred: $status"
       # more code here
       ;;
esac

If you aren't concerned with errors, then just check for a zero-vs-non-zero condition:

if diff file1.txt file2.txt > difference.txt; then
    # exit status was 0, files are the same
else
    # exit status was > 0, files are different or an error occurred
fi

Upvotes: 2

mattkgross
mattkgross

Reputation: 801

The first line sets a variable DIFF_OUTPUT as the output/terminal result of the command diff new.html old.html.

This is called command substitution. You can encapsulate an expression inline by using $(). Think of it as copying the expression into a terminal and running it and then pasting the result straight back into your code.

So, DIFF_OUTPUT now contains the output of the diff of the two files. If the files are identical, then diff will output nothing, thus the variable DIFF_OUTPUT will be assigned an empty string.

${#variable} returns the length of a variable in bash. Thus, if there was no difference between the files, the variable (DIFF_OUTPUT) will be an empty string - which has a length of 0. Thus, ${#DIFF_OUTPUT} == "0", meaning that, if there was a difference in the files, ${#DIFF_OUTPUT} != "0" and your condition is satisfied.

Upvotes: 1

Lee HoYo
Lee HoYo

Reputation: 1267

  DIFF_OUTPUT="$(diff new.html old.html)"

The first line saves the output of a command diff into a variable DIFF_OUTPUT.

${#DIFF_OUTPUT}

and this expression outputs the length of DIFF_OUTPUT. ${#VAR } syntax will calculate the number of characters in a variable

Upvotes: 1

Related Questions