exit status of command in a pipe line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers exit status of command in a pipe line
# 8  
Old 10-18-2007
Bug Can someone explain me whats this code is doing?

This code is too elegant for me, please explain what it does and how?

Quote:
Originally Posted by Perderabo
This depends on the shell. ksh can smash through that wall by moving the "tail -15" to a co-process. I don't know that cleartool command so I'll use "cat /etc/passwd" for my example:
Code:
#! /usr/bin/ksh
exec 4>&1
tail -5 >&4 |&
exec >&p
cat /etc/passwd
exitcode=$?
exec >&- >&4
wait
echo exitcode = $exitcode
exit 0

# 9  
Old 10-18-2007
Quote:
Originally Posted by topcat8
Hi,

I am trying to test the exit status of the cleartool lsvtree statement below, but it doesn't seem to be working due to the tail pipe, which it is testing instead. Is there a way around this without adding a tonne of new code?
[...]
bash and ksh93 have the pipefail option:

Code:
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $?
0
bash 3.2.25(17)$ set -o pipefail
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $?
2


bash and zsh have the PIPESTATUS/pipestatus array/variable:

Code:
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $PIPESTATUS
2
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo ${PIPESTATUS[@]}
2 0

Code:
zsh 4.3.4% ls x|tail
ls: cannot access x: No such file or directory
zsh 4.3.4% echo $pipestatus
2 0

# 10  
Old 10-18-2007
set -o pipefail not available on sunos, $PIPESTATUS works on bash not on ksh

Quote:
Originally Posted by radoulov
bash and ksh93 have the pipefail option:

Code:
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $?
0
bash 3.2.25(17)$ set -o pipefail
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $?
2


bash and zsh have the PIPESTATUS/pipestatus array/variable:

Code:
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo $PIPESTATUS
2
bash 3.2.25(17)$ ls x|tail
ls: cannot access x: No such file or directory
bash 3.2.25(17)$ echo ${PIPESTATUS[@]}
2 0

Code:
zsh 4.3.4% ls x|tail
ls: cannot access x: No such file or directory
zsh 4.3.4% echo $pipestatus
2 0

set -o pipefail doesnt seem to work on SunOS 5.8

Code:
bash-2.03$ uname
SunOS
bash-2.03$ set -o pipefail
bash: set: pipefail: unknown option name

echo $PIPESTATUS works on bash but not on ksh

Code:
bash-2.05$ ls -l rakesh | tail -5
rakesh: No such file or directory
bash-2.05$ echo $PIPESTATUS
2

# 11  
Old 10-19-2007
Quote:
Originally Posted by rakeshou
set -o pipefail doesnt seem to work on SunOS 5.8

Code:
bash-2.03$ uname
SunOS
bash-2.03$ set -o pipefail
bash: set: pipefail: unknown option name

[...]
Yes,
it's a new feature of bash Bash-3.0.

Quote:
echo $PIPESTATUS works on bash but not on ksh

Code:
bash-2.05$ ls -l rakesh | tail -5
rakesh: No such file or directory
bash-2.05$ echo $PIPESTATUS
2

Hm ...., I said bash and zsh.
I said that the pipefail option is available in ksh93 (not ksh88)!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit Status of Command

Hi All, I am doing an export and import (datapump) of 4 schema. I know we can do an export of 4 schema in one command. But just want to know how to check the exit status if i do the export/import of 4 schema in different commands in background. Please suggest. Thanks, Mani (1 Reply)
Discussion started by: pvmanikandan
1 Replies

2. UNIX for Dummies Questions & Answers

Exit Status Of Find Command

Hello All, I am trying to capture the exit status of find command and want to delete the files only when it is successful. But it is always returning me as success even if the pattern of that file doesn't exist in the current directory. please help, checked manual page but couldn't able to figure... (6 Replies)
Discussion started by: Ariean
6 Replies

3. Shell Programming and Scripting

Check the exit status in a pipe call

Guys, I have a problem :confused: and I need some help: I've to process many huge zip files. I'd code an application that receive the data from a pipe, so I can simple unzip the data and send it (via pipe) to my app. Something like that: gzip -dc <file> | app The problem is: How can I... (7 Replies)
Discussion started by: Rkolbe
7 Replies

4. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

5. HP-UX

Logins command exit status is 236 not 0

I have noticed that on version 11.23 I get exit status 236 from the following command: logins -oxl root ; echo $? > 236 However on 11.31 I get the expected code 0 logins -oxl root ; echo $? > 0 The output is correct for both versions and contains no error data. Can anyone explain... (2 Replies)
Discussion started by: parkea2
2 Replies

6. UNIX for Dummies Questions & Answers

Move Command and exit status problem

Hi All, I am using the following code to move files from one folder to another on the remote server: ssh username@server <<EOF cd source_dir find . -type f -name "*.txt" |xargs -n1000 -i{} mv {} dest_dir if then send mail indicating error otherwise echo "success" fi EOF ... (1 Reply)
Discussion started by: visingha
1 Replies

7. Shell Programming and Scripting

How to get exit code in a pipe-lined command?

I have a question about how to get the exit code of the first command when it appears in a pipe-lined command. For example, I have the following script: grep abc dddd | tee -a log if ] then echo "ERROR!" fi In the above script, ] is supposed to test the exit code of "grep abc... (3 Replies)
Discussion started by: pankai
3 Replies

8. Shell Programming and Scripting

Getting the exit status of a remote command

Hi to everyone. How can I get the exit status from a remote command executed with rexec? :eek: machine A has RedHat Linux 9 and the remote machine B has SCO UNIX. Code: rexec -l user -p password host sh /u/files/scripts/seq_cal.sh 2006 08 I want the exit status returned by... (1 Reply)
Discussion started by: zoonalex
1 Replies

9. UNIX for Dummies Questions & Answers

how to find the exit status for the last executed command

I am executing a find command in my script i.e find $2 -type f -name '*.gif' -mtime +$1 -exec rm {} \; how do i check that this command is executed properly.. i would lke t trap the errror and display my error message kinly help.. this is an urgent issue. (1 Reply)
Discussion started by: vijay.amirthraj
1 Replies

10. Programming

How to find the exit status of last command in Unix?

Hi, I want to find the exit status of the last executed command in C Shell. Tried $? but getting the error Variable syntax...$? does not seem to work in C shell.. is there any other command in C shell to find the exit status of last command? Thanks in advance, raju (1 Reply)
Discussion started by: rajugp1
1 Replies
Login or Register to Ask a Question