0

I'm using a bash script to retrieve the differences between two text files:

`

# Do the match between the text files                                                                                                                                                                                                                                                                                  

   echo " "
   echo "match with diff: "
   echo " "
   diff -s FromRucio_$NAME.txt  FromFolder_$NAME.txt
   diff -q FromRucio_$NAME.txt  FromFolder_$NAME.txt

   echo ""
   echo "this is the missing files between the files"
   echo "sample: $NAME \n" >> totalresult_test.txt
   sort FromRucio_$NAME.txt FromFolder_$NAME.txt|uniq -u >> totalresult_test.txt
#  diff --brief <(sort file1) <(sort file2)                                                                                                                                                                                                                                                                               

   echo "_____________________________________________________________________________________"
   echo " "

`

note: the diff commands are executed but when diff -s pass diff -q generate some ouput and vice versa. How can avoid this? i mean, generate a boolean condition when one of them generates an ouput and just pass the next command diff-line.

I would like to create a condition (maybe driven with just a boolean variable) to retrieve the output information from the "sort (...) " line when diff -q command works.

i hope to add new lines to totalresul_test.txt with the sample name and the missing found files from the sort-line bash command.}

Regards

Magoo
  • 77,302
  • 8
  • 62
  • 84
ryse
  • 13
  • 4
  • If you just want to know if two files are the same, and don't care about what exactly is different between them, `cmp` is a better-chosen tool than `diff`. While `diff` does short-circuit when given flags that mean no output is needed, by default it runs a bunch of very slow/expensive work that for your use cases you have no need for. – Charles Duffy Nov 07 '22 at 20:36
  • Also, if you want to know which lines are in one file but not the other, there's a specialized/optimized tool for that job too -- that tool is `comm`. (It requires its inputs to be in sort order; that requirement lets it run in constant memory and O(max(n,m)) time). – Charles Duffy Nov 07 '22 at 20:39

1 Answers1

0

You only need the "diff -q" test for your boolean test.

$? is 1 when different and 0 when identical.

You can build your logic around that.

#!/bin/sh

diff -q test_45.sh test_45_dup.sh >>/dev/null
if [ $? -eq 1 ]
then
    echo "report diff"
else
    echo "no action"
fi
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11