LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Shell - PIPESTATUS (https://www.linuxquestions.org/questions/linux-newbie-8/bash-shell-pipestatus-873374/)

mrm5102 04-06-2011 11:19 AM

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

mrm5102 04-06-2011 02:00 PM

*
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


chrism01 04-06-2011 07:10 PM

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.

mrm5102 04-07-2011 08:40 AM

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?


All times are GMT -5. The time now is 04:50 AM.