Exit status of grep | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Exit status of grep

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-09-2010
prasbala prasbala is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 15 July 2011, 9:13 AM EDT
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
Exit status of grep

I am trying to get the exit status of grep and test a condition with it, But it does not seem to be working as expected since i am doing something wrong apparently

as per grep help
Exit status is 0 if match, 1 if no match,
and 2 if trouble.

My problem is something like this

templine - a string which has isVoid()


Code:
isVoid=`echo $tempLine | grep -o "isVoid\(\)"`
                echo "isVoid: " $isVoid 
                if  [ $isVoid = 0 ]
                then
                    echo "Is Void matched" $isVoid
                fi

When I echo $isVoid, i get the matched string, but i am not sure how to test the exit status of grep. In this case the if condition is not satisfied

Last edited by prasbala; 03-09-2010 at 03:47 AM.. Reason: use code tags please, ty
Sponsored Links
    #2  
Old 03-09-2010
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 8 December 2013, 3:25 AM EST
Location: In the beautiful World...
Posts: 509
Thanks: 9
Thanked 28 Times in 28 Posts
change the if condition as below...


Code:
if  [ $isVoid -eq 0 ]
then
   echo "Is Void matched" $isVoid
fi

= is an assignment operator
Sponsored Links
    #3  
Old 03-09-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 July 2014, 1:54 PM EDT
Location: The Netherlands
Posts: 7,692
Thanks: 120
Thanked 531 Times in 502 Posts
Try:

Code:
isVoid=`echo $tempLine | grep -o "isVoid\(\)"`
if [ $? -eq 0 ]
then
  echo Success
fi



Quote:
Originally Posted by malcomex999 View Post
change the if condition as below...


Code:
if  [ $isVoid -eq 0 ]
then
   echo "Is Void matched" $isVoid
fi

= is an assignment operator
If you grep for "isVoid\(\)" the output of grep should never be 0.
    #4  
Old 03-09-2010
frans's Avatar
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 53 Times in 52 Posts
Mybe this works :

Code:
if isVoid=$(echo $tempLine | grep -o "isVoid\(\)")
then echo "Is Void matched" $isVoid
fi

$() command substitution is preferred to ``
Sponsored Links
    #5  
Old 03-09-2010
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 29 July 2014, 4:51 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,215
Thanks: 120
Thanked 450 Times in 410 Posts
Assigning the substituted output of a command to a variable will simply contain it's output, ie. the pattern grep -o found or not. It does not contain the return code of the grep!

Use Franklins example since it is the only one of that cares for the exit code which is checked by $? immediately after the command you want to observe.

It doesn't have anything to do with the type of how it is substituted ie. double `or $().
Sponsored Links
    #6  
Old 03-09-2010
frans's Avatar
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 53 Times in 52 Posts
Quote:
Originally Posted by zaxxon View Post
Assigning the substituted output of a command to a variable will simply contain it's output, ie. the pattern grep -o found or not. It does not contain the return code of the grep!
The variable contains the output but the return code corresponds to the return code of the last command (grep). Try to print $? and you'll see what happens.

Quote:
Originally Posted by zaxxon View Post
It doesn't have anything to do with the type of how it is substituted ie. double `or $().
Right, it just makes it more readable and permits the nesting.
Sponsored Links
    #7  
Old 03-09-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 July 2014, 1:54 PM EDT
Location: The Netherlands
Posts: 7,692
Thanks: 120
Thanked 531 Times in 502 Posts
Quote:
Originally Posted by frans View Post
The variable contains the output but the return code corresponds to the return code of the last command (grep). Try to print $? and you'll see what happens.
This is what happens:

Code:
$ s=`echo 'abc def'|grep 'xxx'`
$ echo $?                      
1
$ s=`echo 'abc def'|grep 'abc'`
$ echo $?                      
0

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exit Status 126 - how to get rid of it glamo_2312 UNIX for Advanced & Expert Users 5 09-23-2009 12:37 AM
Using exit status of GREP to proceed Great Uncle Kip UNIX for Dummies Questions & Answers 1 02-20-2009 02:33 PM
Exit status MartyIX Shell Programming and Scripting 9 09-09-2008 05:47 AM
How to get the exit status yhacks Shell Programming and Scripting 1 05-19-2008 08:06 AM
ftp exit status. oracle8 UNIX for Advanced & Expert Users 1 10-21-2001 11:34 PM



All times are GMT -4. The time now is 06:42 AM.