The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
output of command rup kiranrasane UNIX for Advanced & Expert Users 1 03-27-2008 07:31 AM
ls command output arsheshadri AIX 1 08-20-2007 03:23 AM
Output of ps -ef command dbrundrett UNIX for Advanced & Expert Users 6 01-06-2005 11:48 AM
Output of ps command ls1429 UNIX for Dummies Questions & Answers 1 02-03-2003 07:40 AM
Output command berty007 Shell Programming and Scripting 4 11-29-2002 02:27 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-22-2007
Registered User
 

Join Date: May 2007
Posts: 59
if command not having the right output

Code:
if cat /var/log/messages | grep fail = " "  
then 
echo "Good"
else
echo "Bad"
fi
i have 10 files named messages.0 , messages.1 , .... till 9 then i need cat all the files and grep to see if there is anything failing. therefore, i used the above command however, it doesn't seems to be working please help.
Reply With Quote
Forum Sponsor
  #2  
Old 05-22-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
You need backquotes to capture output
Then double quotes to make it a single argument for test
And you need test
And it should be a truely empty string

Code:
if test "`cat /var/log/messages | grep fail`" = ""  
then 
echo "Good"
else
echo "Bad"
fi
alternatively use

Code:
if test -z "`cat /var/log/messages | grep fail`" 
...
But you could get rid of all the above with

Code:
if grep fail /var/log/messages
then
     echo failed
else
     echo no problems
fi
Reply With Quote
  #3  
Old 05-22-2007
Registered User
 

Join Date: May 2007
Posts: 211
#!/bin/sh

LOG_FILE=log.txt
grep -l fail /var/log/messages* > $LOG_FILE
if [$? -eq 0]
then
echo "Bad"
echo "Files that contains failures are"
cat $LOG_FILE
else
echo "Good"
fi
exit
Reply With Quote
  #4  
Old 05-22-2007
Registered User
 

Join Date: May 2007
Posts: 59
thank you all for the prompt reply ! All of the info provided is very useful to me thanks !
Reply With Quote
  #5  
Old 05-22-2007
Registered User
 

Join Date: May 2007
Posts: 59
Code:
#!/bin/sh

LOG_FILE=log.txt
grep -l fail /var/log/messages* > $LOG_FILE
if [$? -eq 0]
then 
      echo "Bad"
      echo "Files that contains failures are"
      cat $LOG_FILE
else
      echo "Good"
fi
exit
[$? -eq 0]

$? ==> unknown variable or all variable ?
-eq ==> equals ?
Reply With Quote
  #6  
Old 05-22-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Code:
awk '/[Ff]ail/{filename[FILENAME];nextfile}END{for (i in filename) print i }' /var/log/messages*
Reply With Quote
  #7  
Old 05-22-2007
Registered User
 

Join Date: May 2007
Posts: 211
filthymonk i am just checking the status of the execution of the grep command so i have used ($? variable) and checking for 0
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0