LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-06-2011, 11:19 AM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Rep: Reputation: 3
Bash Shell - PIPESTATUS


Hey all,

I have a bash script that runs this command:
Code:
./myExpTest.exp $1 $2 | tee ~/Documents/Expect_script/new/myTestTee.txt
EXPECT_RETCODE=${PIPESTATUS[0]}
TEE_RETCODE=${PIPESTATUS[1]}

echo $EXPECT_RETCODE
echo $TEE_RETCODE
The command executes (with or without errors) and the exit status echoed to the screen is only the $EXPECT_RETCODE. But it also echos a empty line under the first echo, where the 2nd exit status should be.

Anyone know why it isn't returning the 2nd exit status to me?

I also tried this: (instead of the above example)
Code:
./myExpTest.txt $1 $2 | tee ~/Documents/Expect_script/new/myTestTee.txt
echo ${PIPESTATUS[*]}
echo ${PIPESTATUS[0]}
echo ${PIPESTATUS[1]}
The command above executes and:
the first echo with '*' gives me: "0 0" --> which is correct
the second echo with '0' gives me: "0" --> also correct
the third echo with '1' gives me: " " --> echos nothing


Anyone know why this is happening? Am I using it correctly?

Thanks in advance,
Matt
 
Old 04-06-2011, 02:00 PM   #2
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
*
On further testing I found that the command below will echo both of the exit status' correctly if their on the same line.

Code:
$> echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
0 0
 
Old 04-06-2011, 07:10 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I suspect it's analogous to $?.
There's one and only one copy of this generic cmd completion status var and every cmd issued updates it, so you have to test it immediately after running the cmd you want to check. The act of testing it is another cmd, so the status changes again.
 
Old 04-07-2011, 08:40 AM   #4
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
Hey Chris,

Thanks for the response.
I get that $? changes/overwritten on any cmd given, that's what led me to find the PIPESTATUS array variable in the bash shell. Is the $? var related to the PIPESTATUS var?
Basically I just want to get the exit status of the 2 given commands using the PIPESTATUS variable and store it in my own variable to do some conditional statements on it. What would be the best way to do that?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about shell (bash shell) thomas2004ch Linux - Newbie 4 08-27-2009 02:09 AM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 01:24 AM
own shell in place of bash shell somu_thedev Programming 6 07-31-2007 10:13 AM
Run a C Shell using only Bash shell HSN Linux - Software 1 12-25-2006 07:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration