Visit the LQ Articles and Editorials section
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-25-2012, 10:23 AM   #1
LQ Newbie
Registered: Jun 2012
Posts: 11

Rep: Reputation: Disabled
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
cat File A

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

Scenario 2
cat File A

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

Scenario 3
cat File A

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

Can anybody shed some light on how to accomplish this.

Thanks Much
Old 07-25-2012, 10:36 AM   #2
Registered: Jun 2009
Distribution: Redhat,CentOS,Ubuntu,Puppet
Posts: 292

Rep: Reputation: 34
It can be compared & matched with md5sum fileA & md5sum fileB or diff command
Old 07-25-2012, 10:44 AM   #3
Senior Member
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,247

Rep: Reputation: 312Reputation: 312Reputation: 312Reputation: 312
What about order of values, are both files always sorted the same way? For example, what should happen on this:
cat File A

cat File B
And what about white spaces?
Old 07-25-2012, 04:25 PM   #4
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Adding to Smilemukul's comment you may also want to check out the comm command in bash it has some nice switches.
Old 07-25-2012, 05:14 PM   #5
Paddy Landau
LQ Newbie
Registered: Jul 2012
Location: United Kingdom
Distribution: Ubuntu
Posts: 2

Rep: Reputation: Disabled
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:

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script count files in the directory but keep loop not exit untill see 10 files. dotran Linux - Newbie 9 12-22-2014 04:34 AM
script to compare users in files s_linux Programming 7 04-05-2011 10:34 AM
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 08:32 AM
How to Compare Two files using shell script pooppp Linux - Networking 14 08-05-2008 03:35 AM
How do I compare two values in separate files xmrkite Linux - Software 11 09-19-2007 11:36 PM

All times are GMT -5. The time now is 04:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration