LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Want to retrieve exit code of one script into another (http://www.linuxquestions.org/questions/programming-9/want-to-retrieve-exit-code-of-one-script-into-another-929249/)

prravin1 02-14-2012 04:15 AM

Want to retrieve exit code of one script into another
 
Hello,

I have two shell scripts. I am creating a pipeline in which these two scripts will execute in sequential order. But I want to make sure that if the first scripts fails, the second script should not execute. How can I retrieve the exit code of first script into second script?

+++++++++++++++++++++++++++++++++++++++++++++++++++++
Suppose I have two scripts:
A.sh
B.sh

Now I don't want a third script to invoke this two scripts.

I want to retrieve the exit status code of A.sh in B.sh and then start executing it.

Before starting B.sh, will it be possible for it to check the exit code of previously executed script (A.sh) in B.sh itself.

Thanks.

druuna 02-14-2012 04:22 AM

Hi,

It depends a bit on the exit code you are revering to, but try this:
Code:

a.sh && b.sh
The && tells the shell to only execute b.sh if a.sh was successful. Executing b.sh if a.sh was unsuccessful is also possible:
Code:

a.sh || b.sh
Hope this helps.

prravin1 02-14-2012 04:41 AM

cont....
 
I tried it before. But actually i am executing these scripts from a GUI. So these two scripts are located at a common location. The application will pick these scripts from the location.

Say it will pick A.sh and start executing it. Now if its successful then only it should be also to run B.sh.

I need a bit of code in the script B.sh which will check the exit status code of A.sh in the script itself.

In short, any script running after the previous script should check the successful execution of the previous script and then proceed.

hope it helps....

druuna 02-14-2012 04:57 AM

Hi,
Quote:

Originally Posted by prravin1 (Post 4601920)
I tried it before. But actually i am executing these scripts from a GUI. So these two scripts are located at a common location. The application will pick these scripts from the location.

Say it will pick A.sh and start executing it. Now if its successful then only it should be also to run B.sh.

I need a bit of code in the script B.sh which will check the exit status code of A.sh in the script itself.

In short, any script running after the previous script should check the successful execution of the previous script and then proceed.

hope it helps....

In this case the script(s) aren't aware of each others exit codes. You need to add a piece of code to the a.sh script that stores the exit code in a (temporary) file and let script b.sh check that file.

Simple example:
Code:

# a.sh
#!/bin/bash
ECODE="0"

ls -l asdfasdf
ECODE="$?"

echo $ECODE > /tmp/a.exit.code

# b.sh
#!/bin/bash

exitcode="`cat /tmp/a.exit.code`"

[[ "$exitcode" != "0" ]] && exit

echo "a.sh ran without error"

Hope this helps.

prravin1 02-14-2012 05:01 AM

Thanks for your reply. Will try this solution and will get back to you after the execution.


All times are GMT -5. The time now is 02:07 AM.