LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2012, 04:15 AM   #1
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Rep: Reputation: Disabled
Unhappy 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.
 
Old 02-14-2012, 04:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 02-14-2012, 04:41 AM   #3
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Unhappy 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....
 
Old 02-14-2012, 04:57 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by prravin1 View Post
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.
 
1 members found this post helpful.
Old 02-14-2012, 05:01 AM   #5
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Thanks for your reply. Will try this solution and will get back to you after the execution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find the exit code of a program from within a shell script?? yanom Programming 2 09-30-2011 11:47 AM
How do i run a part of code on exit in a shell script? saiteju Programming 2 05-14-2010 05:22 AM
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 08:32 AM
script exit code to system() c function nemobluesix Programming 4 07-06-2009 05:56 AM
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 03:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration