LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-14-2012, 09:31 PM   #1
prl77
LQ Newbie
 
Registered: May 2011
Posts: 6

Rep: Reputation: 0
Catch exit code before pipe in bash script?


I have a bash script that runs a command and pipes its output to 'tee' which splits it into two log files for later parsing. I need to catch the exit code from the command right before piping. How?

Code:
/usr/bin/command1 parameter1 --option1 2>&1 | tee -a $LOG1 $LOG2
        if [ $? != 0 ]; then
                do some shit
        else
                do other shit
        fi
It appears that I'm catching the result of 'tee' and not of 'command1'.

Thanks,
prl77

PS. I'm going on vacation tomorrow, will respond back in a week.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-14-2012, 09:44 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
The bash variable PIPESTATUS:
Quote:
An array variable (see Arrays) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command).
Code:
c@CW8:~$ false | true | echo foo
foo
c@CW8:~$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]} ${PIPESTATUS[2]}
1 0 0
 
2 members found this post helpful.
Old 05-14-2012, 09:47 PM   #3
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
Remember, '$?' always returns the exit status of the last command, in your case the 'tee' command. What you can do, is save the output of command1 to a var first. You must also save the exit status to another var first if tee must be executed before the conditionals.

Hope it points you in the right direction.
 
Old 05-14-2012, 09:48 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
Aha, PIPESTATUS, something new for me learn about.
 
Old 05-24-2012, 12:09 PM   #5
prl77
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
The bash variable PIPESTATUS:
Code:
c@CW8:~$ false | true | echo foo
foo
c@CW8:~$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]} ${PIPESTATUS[2]}
1 0 0
This hit the spot. Thanks man!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash script] How to catch the return string? thomas2004ch Linux - Software 3 02-27-2012 09:06 AM
[SOLVED] Want to retrieve exit code of one script into another prravin1 Programming 4 02-14-2012 05:01 AM
Bash script: catch file not found error and send to /dev/null noir911 Programming 7 04-24-2010 08:37 PM
script exit code to system() c function nemobluesix Programming 4 07-06-2009 05:56 AM
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 03:07 AM


All times are GMT -5. The time now is 08:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration