BASH - Propagation of error codes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - Propagation of error codes
# 1  
Old 02-19-2015
BASH - Propagation of error codes

Hello.

I use bash functions which are in different script files.
Theses scripts function files are sourced when necessary.
As function does not easily return string value, Value are return using the echo function, and a return code is returned..

Now here my question :
In the main script I want to get the error code which is 0 if every things run correctly, otherwise it is different from 0.
As in my example "function2 return always 2, I expect an error code different from 0.
But I always get 0 ( success ).
I suppose because the last function in the pipe (not_important_function_b) always success and then set the return code to 0. Even if the first part of the pipe failed.
Code:
VERSION_VALUE=$( function2 "$MY_TEMPFILE" "a_string" "another_string" | not_important_function_b )

How to catch the error from function2.

Any help is welcome.

Here the skeleton of my problem.

Code:
#!/bin/bash
# FILE : file2
#
function function_2 {
not_important_function_c "$1" "$2" "$3" if [[ ! -z "$3" ]] ; then
CODE_ERR = 0 echo "GOOD_VALUE"
else
CODE_ERR = 1
fi CODE_ERR = 2 retrun $CODE_ERR # always return 2
}

Code:
#!/bin/bash
# FILE : file1
#
function function_1 {
#
    MY_TEMPFILE=$(mktemp)
    CODE_ERR_GLOB=$?  # never fail --> 0 success
#
    not_important_function_a "$1"  "$2"  > "$MY_TEMPFILE"
    CODE_ERR=$?  # never fail --> return 0 success
    (( CODE_ERR_GLOB += CODE_ERR ))
#
    VERSION_VALUE=$( function2 "$MY_TEMPFILE" "a_string" "another_string" | not_important_function_b )  
# function2 always fails -> return 2
# not_important_function_b never fails --> return 0 success
    echo "$?"
    CODE_ERR=$? # always 0 --> success but function2 fails
    echo "CODE_ERR : $CODE_ERR"
    (( CODE_ERR_GLOB += CODE_ERR ))
    echo "CODE_ERR_GLOB : $CODE_ERR_GLOB"
#
    rm -f $MY_TEMPFILE > /dev/null 2>&1
    CODE_ERR=$? # always 0 --> success but function2 fails
    (( CODE_ERR_GLOB += CODE_ERR ))
#
    if [[ $CODE_ERR_GLOB -eq 0  ]] ; then
        echo $VERSION_VALUE # echo a good value if no error nowhere
    fi
#
    return $CODE_ERR_GLOB always success but function2 fails
#
}

Code:
#!/bin/bash
#
#Main_program
#
source file1
source file 2
source file 3
#
MY_DATA=$(function_1 "param1" "param2" "param3")
ERR_CODE=$?     # always = 0 ! ! ! ! !
if [[  $ERR_CODE -ne 0 ]] ; then
echo "Error from function 1" # never print this
else
echo "Good data : $MY_DATA"
fi

This User Gave Thanks to jcdole For This Post:
# 2  
Old 02-19-2015
Hi,
In your code function_2, you have write retrun between return

Regards.
# 3  
Old 02-19-2015
Quote:
Originally Posted by disedorgue
Hi,
In your code function_2, you have write retrun between return

Regards.
It is just a typo in this thread.

These script are just shown to explain the question.
The code by itself is not important.

In the coding line
Code:
VERSION_VALUE=$( function2 "$MY_TEMPFILE" "a_string" "another_string" | not_important_function_b )

If the last part of a pipe return no error and if a previous part of a pipe return one error, how to get the error code for the global result which must be considered as failing.
# 4  
Old 02-19-2015
Code:
    echo "$?"
    CODE_ERR=$? # always 0 --> success but function2 fails

echo is executed successfully, so upon next check with $? it is 0.
You probably want:
Code:
CODE_ERR=$?
echo $CODE_ERROR

hth

EDIT:
Thanked for the idea of making a function to print out a just created tempfile Smilie Smilie
# 5  
Old 02-19-2015
Quote:
Originally Posted by sea
Code:
    echo "$?"
    CODE_ERR=$? # always 0 --> success but function2 fails

echo is executed successfully, so upon next check with $? it is 0.
You probably want:
Code:
CODE_ERR=$?
echo $CODE_ERROR

hth
It is just one more typo in this thread.
# 6  
Old 02-19-2015
PS:
In your main script, maybe you could try something like:
Code:
if MY_DATA=$(function_1 "param1" "param2" "param3")
then echo "yay : $MY_DATA"
else err=$?
     echo "no : $err"
fi

hth

EDIT:
Your "typos" include beeing on the wrong line?
gee use the real one or care more about the example representing properly your question with working example... like... many of your code looks almost like incomplete csh, rather than bash.
# 7  
Old 02-19-2015
My real program is working correctly.
These script are just shown to explain the question.
The code by itself is not important.

My real problem is the coding line
Code:
 VERSION_VALUE=$( function2 "$MY_TEMPFILE" "a_string" "another_string" | not_important_function_b )
RETURN_CODE=$?

If the last part of a pipe return no error and if a previous part of a pipe return one error (say 123)
1°) What is the value of $RETURN_CODE 0 or 123
2°) Is the return code set to 0 ( if not, I have a problem somewhere in my code)
how to get the error code (123) for the global result which must be considered as failing.

Thank you for helping
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