LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-27-2011, 03:55 AM   #1
Markus72
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
diff - how do i read it as true or false?


Hi,

I'm sorry for this basic question. I can't for the life of me remember how to do it and no lack of Google searches has found me an answer.

I simply wish to compare 2 files using diff and perform an action if they are different (e.g. move the file that's different).

I'm using:-

diff -q file01 file02

and get:-

Files file01 and file02 differ

Does the diff -q produce some kind of standard out that I can use in a simple if statement, such as:-

if [ diff -q file01 file02 ] then
echo Files different
else
echo Files same
fi


I did actually try that, but of course didn't work . Once again sorry for the simple question, I'm expecting to kick myself once I find out how it's done .

Cheers,

KISA
 
Old 03-27-2011, 04:05 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The exit status (0 for not different, 1 for different) of the diff command can be used.
Code:
#!/bin/bash

diff -q file1 file2 1>/dev/null
if [[ $? == "0" ]]
then
  echo "The same"
else
  echo "Not the same"
fi
I added the 1>/dev/null to suppress all the output, which is probably not needed.

Hope this helps.
 
Old 03-27-2011, 04:11 AM   #3
Markus72
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the quick reply druuna, that's exactly what I was after.

Cheers.
 
Old 03-27-2011, 04:13 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome

BTW: Can you put up the [SOLVED] tag (first post -> Thread Tools).
 
  


Reply



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
UpStart script : create folder - true ; run virtualbox - false. wertum Linux - General 1 08-17-2010 05:57 PM
if statement ignoring true/false and proceeding anyway. Goblin_C_Noob Programming 4 03-30-2008 09:56 AM
comparison is always true/false jubaitca Programming 20 11-05-2006 06:55 PM
true or false? alaios Programming 7 07-16-2005 10:54 AM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:00 AM.

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