LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   LINUX - How to compare the values in 2 files & exit from the script (http://www.linuxquestions.org/questions/linux-newbie-8/linux-how-to-compare-the-values-in-2-files-and-exit-from-the-script-4175418609/)

dsfreddie 07-25-2012 10:23 AM

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
Quote:

cat File A
20120701,20120702,20120703

cat File B
20120701,20120702
In this case, the script should exit without proceeding further.

Scenario 2
Quote:

cat File A
20120701,20120702,20120703

cat File B
20120701,20120702,20120703
In this case, the script should proceed further.

Scenario 3
Quote:

cat File A
20120701,20120702,20120703

cat File B
20120701,20120702,20120704
In this case, the script should exit.

Can anybody shed some light on how to accomplish this.

Thanks Much
Freddie

smilemukul 07-25-2012 10:36 AM

It can be compared & matched with md5sum fileA & md5sum fileB or diff command

eSelix 07-25-2012 10:44 AM

What about order of values, are both files always sorted the same way? For example, what should happen on this:
Quote:

cat File A
20120701,20120702,20120703

cat File B
20120703,20120701,20120702
And what about white spaces?

Sydney 07-25-2012 04:25 PM

Adding to Smilemukul's comment you may also want to check out the comm command in bash it has some nice switches.

Paddy Landau 07-25-2012 05:14 PM

The diff command returns zero if the files are the same, or non-zero if they differ.

You can use an if-statement, or just the following single line:

Code:

diff 'File A' 'File B' || exit


All times are GMT -5. The time now is 05:00 PM.