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 and shell scripting languages 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 10:31 AM
ls command output arsheshadri AIX 1 08-20-2007 07:23 AM
Output of ps -ef command dbrundrett UNIX for Advanced & Expert Users 6 01-06-2005 02:48 PM
Output of ps command ls1429 UNIX for Dummies Questions & Answers 1 02-03-2003 10:40 AM
Output command berty007 Shell Programming and Scripting 4 11-29-2002 05:27 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2007
filthymonk filthymonk is offline
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.
  #2 (permalink)  
Old 05-23-2007
porter porter is offline Forum Advisor  
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

  #3 (permalink)  
Old 05-23-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
#!/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
  #4 (permalink)  
Old 05-23-2007
filthymonk filthymonk is offline
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 !
  #5 (permalink)  
Old 05-23-2007
filthymonk filthymonk is offline
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 ?
  #6 (permalink)  
Old 05-23-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,558

Code:
awk '/[Ff]ail/{filename[FILENAME];nextfile}END{for (i in filename) print i }' /var/log/messages*

  #7 (permalink)  
Old 05-23-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
filthymonk i am just checking the status of the execution of the grep command so i have used ($? variable) and checking for 0
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0