BASH - Propagation of error codes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - Propagation of error codes
# 8  
Old 02-19-2015
To my understanding it goes like:

If function2 FAILS, as in a code typo or floating number exception, then the pipe will be ignored and therefor the RETURN_CODE is the exception number of function2.
However, if function2 'ends' properly with an exit number, whatever comes out (or not) stdout will be passed to not_important_function_b which therfor will 'overwrite' the previous return value $? of function2, by its own value - as you report - 0.

hth

Last edited by sea; 02-19-2015 at 12:06 PM.. Reason: Typo
# 9  
Old 02-19-2015
Ok,
In bash, you can propage error in pipe by setting pipefail:
Code:
$ false | true
$ echo $?
0
$ set -o pipefail
$ false | true
$ echo $?
1

Regards.
This User Gave Thanks to disedorgue For This Post:
# 10  
Old 02-19-2015
Try the PIPESTATUS array:

Code:
$ true | false | true | false
$ echo ${PIPESTATUS[*]}
0 1 0 1

To save $PIPESTATUS :

Code:
$ true | false | true | false
$ foo=( ${PIPESTATUS[*]} )
$ echo ${keepme[*]}
0 1 0 1

This User Gave Thanks to jim mcnamara For This Post:
# 11  
Old 02-19-2015
if your shell is not bash, then...
Code:
 with any other Bourne like shells

       You need to use a trick to pass the exit codes to the main
       shell.  You can do it using a pipe(2). Instead of running
       "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
       to the shell.

       exec 3>&1
       eval `
         # now, inside the `...`, fd4 goes to the pipe
         # whose other end is read and passed to eval;
         # fd1 is the normal standard output preserved
         # the line before with exec 3>&1
         exec 4>&1 >&3 3>&-
         {
           cmd1 4>&-; echo "ec1=$?;" >&4
         } | {
           cmd2 4>&-; echo "ec2=$?;" >&4
         } | cmd3
         echo "ec3=$?;" >&4
       `

This User Gave Thanks to vgersh99 For This Post:
# 12  
Old 02-23-2015
Quote:
Originally Posted by vgersh99
if your shell is not bash, then...
Code:
 with any other Bourne like shells

       You need to use a trick to pass the exit codes to the main
       shell.  You can do it using a pipe(2). Instead of running
       "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
       to the shell.

       exec 3>&1
       eval `
         # now, inside the `...`, fd4 goes to the pipe
         # whose other end is read and passed to eval;
         # fd1 is the normal standard output preserved
         # the line before with exec 3>&1
         exec 4>&1 >&3 3>&-
         {
           cmd1 4>&-; echo "ec1=$?;" >&4
         } | {
           cmd2 4>&-; echo "ec2=$?;" >&4
         } | cmd3
         echo "ec3=$?;" >&4
       `

I am running bash 4.2.53

---------- Post updated at 09:56 ---------- Previous update was at 09:39 ----------

Thank you every body for helpingSmilie

Last edited by jcdole; 02-23-2015 at 04:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Error Codes for VCS

Do we have common VCS error codes for all platforms. eg. 10195 Agent(s) for group %s failed on system %s for all Linux,Solaris and windows ? (1 Reply)
Discussion started by: NIMISH AGARWAL
1 Replies

2. Shell Programming and Scripting

rm error codes and logging

Afternoon ladies and gents, I am trying to create a simple script to remove a certain file from a user's network profile location. The removal works ok, but in the interest of overkill I would like to add a simple error detection (such as file doesn't exist or permission denied) Currently, it... (2 Replies)
Discussion started by: hateborne
2 Replies

3. Shell Programming and Scripting

Bash Shell Script Exit Codes

Here is my daily stupid question: How can I tell a script to only execute if the other scripts exits successfully? So "script A" executes and it executes successfully (0),then "script B" will run or else "script A "executes and it exits unsucessfully (1) then "script B" will read return... (6 Replies)
Discussion started by: metallica1973
6 Replies

4. Shell Programming and Scripting

ssh use of error codes

I want to import the info to shell when the connection to remote host was closed . I have follwed by ssh errors but how to use variables in script. I am thinking out loud the shell script could look as follow: svnvaraible=$ERROR_SSH_CONNECTION_LOST if ; then break fi (1 Reply)
Discussion started by: Michal Janusz
1 Replies

5. Shell Programming and Scripting

'watch' not interpreting escape codes in bash script

Hi there, I'm fairly new to bash scripting and already having some troubles. I'm making a script that can print some series of strings in colors based in the information of a file, for simplicity let's say it only does: #!/bin/bash printf "\eWhen you execute this in the command line it... (1 Reply)
Discussion started by: Arashi
1 Replies

6. Shell Programming and Scripting

Strange SIGINT propagation between Parent/Child sh scripts

Good day, I am trying to add signal handling capabilities to some of my scripts. Unfortunately, I am having some difficulty with the manner in which signals are propagated between parent/child processes. Consider the following example: I have the following "parent" script: #!/usr/bin/sh... (5 Replies)
Discussion started by: danie.ludick
5 Replies

7. Shell Programming and Scripting

bash awk codes to perl

Hi, I am interesting in writing the following bash codes into perl My script is simple take field 2 in /etc/passwd and put into an array #!/bin/bash PASSWD_FILE=/etc/passwd A=(`awk -F: ' { print $2 }' $PASSWD_FILE `) Can someone give me equivalent codes in perl ? (1 Reply)
Discussion started by: phamp008
1 Replies

8. Shell Programming and Scripting

Ctrl-C signal propagation

Hello I have a master startup script (let's call it myScript) that displays a menu from which the user can start/stop several instances of a server. When I issue the start command for one of the servers from the menu and then exit myScript through the provided mechanism (enter "q" in this case),... (2 Replies)
Discussion started by: EvilBoz
2 Replies

9. AIX

pSeries 610 error codes

I have been given some pSeries AIX servers to maintain. One of the servers wont come up after a shutdown and the following code is showing on the server: 10118401 How do I look up the error code? (2 Replies)
Discussion started by: dangral
2 Replies

10. Shell Programming and Scripting

How to get exit status codes in bash from Perl?

I apologize if I have already posted this query. I scanned back quite a few pages but could not find such a query. If my perl code contains "exit(33)" how can I get that value in bash for use in a "if" statement. Thanks, Siegfried (5 Replies)
Discussion started by: siegfried
5 Replies
Login or Register to Ask a Question