Register
It is currently Sat Dec 20, 2014 12:14 pm

error status when using /dev/null or grep -q?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Oct 30, 2008 9:24 am   

Joined: Thu Oct 30, 2008 9:19 am
Posts: 2
This is a really basic question, but I'm confused by this:

CAFHCServer:~ cafhcserveruser$ diskutil list | grep disk1 | grep -v "partition_scheme"
/dev/disk1
0: *0.0 B disk1
CAFHCServer:~ cafhcserveruser$ echo $?
0
CAFHCServer:~ cafhcserveruser$ diskutil list | grep disk1 > /dev/null | grep -v "partition_scheme" > /dev/null
CAFHCServer:~ cafhcserveruser$ echo $?
1

Why does the 2nd command generate error status 1 simply because I added stdout redirection? The same thing happens when using the "-q" option with grep instead of redirecting to /dev/null.


Top
 Profile  
 PostPosted: Thu Oct 30, 2008 6:57 pm   

Joined: Wed Sep 24, 2008 11:32 pm
Posts: 7
In the second command, you pipe the result " grep disk1 " to /dev/null, so there is nothing to be piped to the "grep -v "partition_scheme"".
That's why it generate error status in my humble opion.


Top
 Profile  
 PostPosted: Fri Oct 31, 2008 5:44 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
from grep's man page...

Quote:
Normally, exit status is 0 if selected lines are found and 1 otherwise.


$? reports the exit status of the last command only, as the last command cannot match any lines grep exits with 1.


Top
 Profile  
 PostPosted: Fri Oct 31, 2008 9:45 am   

Joined: Thu Oct 30, 2008 9:19 am
Posts: 2
Oh duh, of course. Thanks so much for explaining! I feel kind of silly for staring at it for so long and not figuring it out, but a solution is a solution. :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP