LINUX - How to compare the values in 2 files & exit from the script

Hi All,
I have a requirement where I need to compare 2 files & if the values in the files match, it should proceed, else exit the script without proceeding further.
For e.g : Scenario 1

In this case, the script should exit without proceeding further.
Scenario 2

In this case, the script should proceed further.
Scenario 3

In this case, the script should exit.

PLS NOTE : The date values in File A & B can be in any order. So, i think a diff command might not work.

Can anybody shed some light on how to accomplish this.

Thanks Much
Freddie

Does that mean that the following is considered a match?

cat File A
20120701,20120703,20120702
cat File B
20120703,20120702,20120701

Regards,
Alister

Thanks Allister for your reply :slight_smile:

Yes, the example you gave is considered a match.

Regards,
Freddie

The following changes each comma into a newline, effectively converting the file to one that has one date per line. The lines are then sorted and the results are silently compared. cmp will not generate any output, but its exit status can be used to make the decision.

cmp -s <(tr , \\n < file1 | sort) <(tr , \\n < file2 | sort)

If you prefer using temporary files over process substitution:

tr , \\n < file1 | sort > file1.sorted
tr , \\n < file2 | sort > file2.sorted
cmp -s file1.sorted file2.sorted
rm file1.sorted file2.sorted

Regards,
Alister

Thanks Alister, that worked like a champ :slight_smile: I am currently testing my script after incorporating this. Will let you know the outcome...

Regards,
Freddie

Hi Alister,

I am getting a syntax error message while trying to compare 2 files using the compare function (LINUX)

cmp -s <(tr , \\n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \\n < $COMMON_TMP/nt_per_chg_indx.done | sort)

Error Message:

 
command substitution: line 79: syntax error near unexpected token `('
command substitution: line 79: `cmp -s <(tr , \n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \n < $COMMON_TMP/nt_per_chg_indx.done | sort)'
+ file_cmp_chk=1
 
$ cat /iis_dev_data3/wcc/cpmg/tmp/wcc_cpmg_nt_per_chg_indx.done
20120708,20120707
$cat /iis_dev_data3/wcc/cpmg/tmp/wcc_cpmg_nt_per_gs.done
20120708,20120707

Can you pls help to fix this issue.

Thanks
Freddie

Please post the actual line from your script. Unlear where the mismatched leading backtick and trailing vertical quote in this error message came from.

Have you tried @alister's second version? This avoids very modern Shell syntax.
What Operating System and version are you running and what vintage of bash is this?

@dsfreddie
Though we note that you have changed the Shebang line from #!/bin/ksh to #!/bin/bash , please post the exact command line used to invoke the script. This is because you can quite easily override the Shebang line by invoking a script incorrectly.