Visit Our UNIX and Linux User Community

Exit code from piping in unix shell script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit code from piping in unix shell script
# 1  
Old 06-08-2012
Exit code from piping in unix shell script

Hi ,

I have following code in my shell script :
"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p1.stx" "${TS_LOGS}/tranfrmr_p1.err" | (
"$TS_BIN/cusparse" "${TS_SETTINGS}/cusparse_p2.stx" "${TS_LOGS}/cusparse_p2.err" | (
"$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforpm_p3.stx" "${TS_LOGS}/srtforpm_p3.err" | (
"$TS_BIN/uspmatch" "${TS_SETTINGS}/pmatch_p4.stx" "${TS_LOGS}/pmatch_p4.err" | (
"$TS_BIN/winkey" "${TS_SETTINGS}/winkey_p5.stx" "${TS_LOGS}/winkey_p5.err" | (
"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p6.stx" "${TS_LOGS}/tranfrmr_p6.err" | (
"$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforrl_p7.stx" "${TS_LOGS}/srtforrl_p7.err" | (
"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p8.stx" "${TS_LOGS}/tranfrmr_p8.err")))))))

"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p9.stx" "${TS_LOGS}/tranfrmr_p9.err"
"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p10.stx" "${TS_LOGS}/tranfrmr_p10.err"
"$TS_BIN/rellink" "${TS_SETTINGS}/rellinkref_p11.stx" "${TS_LOGS}/rellinkref_p11.err"
"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p12.stx" "${TS_LOGS}/tranfrmr_p12.err" | (
"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p13.stx" "${TS_LOGS}/tranfrmr_p13.err")

"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p14.stx" "${TS_LOGS}/tranfrmr_p14.err"
"$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforrl_p15.stx" "${TS_LOGS}/srtforrl_p15.err"

I want to capture the status code for each each command.
Suppose my below process fails and return status code is non zero

"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p6.stx" "${TS_LOGS}/tranfrmr_p6.err"

than successor commands shouldn't get executed and script should get exited with non zero return status code.

Can anybody help me on that..

Last edited by Scrutinizer; 06-08-2012 at 08:42 AM.. Reason: code tags
# 2  
Old 06-08-2012
What Shell is this?
What is the purpose of the pipes and parentheses on some (but not all) of the lines? I've never seen a construct like this.
# 3  
Old 06-08-2012

This is a bash shell .
$TS_BIN/tranfrmr is script and ${TS_SETTINGS}/tranfrmr_p6.stx is our setting file stored in setting directory where we describe the input and output file
and "${TS_LOGS}/tranfrmr_p6.err" is the error file stored in log directory

When below process runs

"$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p6.stx" "${TS_LOGS}/tranfrmr_p6.err"

the output file generated by above command will be input file for the sucessor command i.e

"$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforrl_p7.stx" "${TS_LOGS}/srtforrl_p7.err"

and so on...
# 4  
Old 06-08-2012
Does it need the pipeline at all?

Are you looking to detect an error exit status from each script, or to processes the ".err" files somehow?
# 5  
Old 06-08-2012
Yes it need the pipeline ,because it is making our process faster,

We are basically looking for error exit status after each command . Actually when we are running the script our job is not capturing the failed exit status code after each command. while running whole of the process and it is giving status exit code 0

For example I have changed the setting file '

tranfrmr_p6.stx' and there is some syntax error or suppose i have changed the input file name in the file
than process should get exited with error code,but successor get run.

Please find the snapshot shot of log when the process is running .

STEP : p6_tranfrmr_log.txt
Fri Jun 8 06:23:29 2012 - Opening Transformer process.

STEP : p7_srtforrl_log.txt
Fri Jun 8 06:23:29 2012 - Opening TSQSORT process.
Fri Jun 8 06:23:34 2012 - Closing TSQSORT process.

In above case STEP :P6 is not able to process the input file so it skipped and moved the next sucessor process
STEP : p7_srtforrl

Also please find the content of the setting file (tranfrmr_p6.stx) .
Please find top and last content as it is a very long xml code


/ ** Long XML code */


# 6  
Old 06-08-2012
the output file generated by above command will be input file for the sucessor command

Yes it need the pipeline ,because it is making our process faster,
Are you passing any data at all down any of the pipelines?
Sorry to labour this point but it would be so much easier to check for errors at each stage if there was no pipeline.
# 7  
Old 06-08-2012

> ls -al | bogus_command
bash: bogus_command: command not found
> echo ${PIPESTATUS[1]}

PIPESTATUS is an array, each element of the array has the reutrn code for the corresponding pipe.

FWIW - your code is truly unmaintainable. If we had production code like that here we would absolutely require it to be refactored.
This User Gave Thanks to jim mcnamara For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #324
Difficulty: Medium
The IBM PC used an Intel 8008 microprocessor clocked at 4.77 MHz and 8 kilobytes of memory.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit code 267 from shell script

Hi, We have a problem in Linux (GNU/Linux 3.10.0-693.1.1.el7.x86_64) with a shell script returning 267 as return code. The script, is called from (both script samples given below). The exit code from is used to decide whether to continue execution of... (1 Reply)
Discussion started by: Arunnath
1 Replies

2. UNIX for Dummies Questions & Answers

UNIX exit code 11

We have a batch Unix process that runs during the day and it is getting an exit code 11 from Unix. It finishes a sqlplus step and gets the exit code 11 before it starts the next step. This used to happen once a year and now is happening more often (but not every time the process runs). We have... (2 Replies)
Discussion started by: msol
2 Replies

3. Shell Programming and Scripting

Exit the shell script

Hi, suppose my script is i have to run using '. ./' as . ./script file always executes the script in my parent shell. when my contains exit command .. my environment is getting closed as am executing in the parent shell ... please suggest me how can i use... (5 Replies)
Discussion started by: pracheth
5 Replies

4. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

5. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

6. Shell Programming and Scripting

How to exit a shell script if a unix command does not return any value for 10 seconds?

Hi, Can anyone help me how to exit a shell script if a unix command inside does not return any value for 10 seconds? The scenarios is like this. I want to login to a application using shell script where the connection string is mentioned.but suppose this connection string is not... (10 Replies)
Discussion started by: arijitsaha
10 Replies

7. Shell Programming and Scripting

exit shell from a script

hi guys I have a script that I need to terminate or exit the shell or session completely for the user but the exit only exit from the script and takes the user to the shell I found this saying that... (1 Reply)
Discussion started by: kopper
1 Replies

8. Shell Programming and Scripting

problem with exit code when piping

i am writing a script to perform some mysqldumps and gzip them. The problem I am running into is that if the user specifies a database that doesn't exist, the error the mysql engine produces is still piped into gzip, and the exit code returned is 0. If I don't pipe into gzip, an exit code... (4 Replies)
Discussion started by: bitoffish
4 Replies

9. Programming

exit status running java classpath in unix shell

I have a java classpath running inside of a unix shell script. During my testing it will error with lines that show an example like this below. error at Method) at<init>(, ... (2 Replies)
Discussion started by: mmcds
2 Replies

10. Shell Programming and Scripting

exit a shell script!!

could somebody tell me please how to exit a shell script: if then echo "No arguments detected" exit 1 fi ... echo "still there" # is displayed .. :-( (4 Replies)
Discussion started by: sami98
4 Replies

Featured Tech Videos