[SOLVED] Want to retrieve exit code of one script into another
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.