Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unix script, how to pass return code back to calling script(s)

Posted on 2013-01-02
10
Medium Priority
?
692 Views
Last Modified: 2013-01-08
I have 4 scripts called in succession, and need to pass the return code back from each called script to the calling script. How can I do that ? I have the return code, just don't know how to receive it in the calling script.

Currently:
My_Calling_Script A, last step:

     my_called_script.ksh $parm1 $parm2

     (how do I receive back $return_code)

-----
my_called_script:

     return $return_code (I'll be adding this new to my script)
0
Comment
Question by:Alaska Cowboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 700 total points
ID: 38738065
The returncode is in the variable. $?
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 38738094
Ok, let me give it a shot, thank you.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 700 total points
ID: 38738122
my_called_script.ksh $parm1 $parm2
RC=$?
echo Returncode of my_called_script.ksh is $RC

if [[ $RC -ne 0 ]]; then
 echo Script failed
   else
     echo Success
fi
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 38738186
I have this which works fine:

$my_called_script.ksh <$maindir/dirList.lst $this_subj_area $return_code

at the end I say

return $return_code

but in the called script it's not working:

   $my_called_script.ksh $maindir $this_source $this_Subject_Area
   return_code = $?
   echo 'Clean verify, return code = ${return_code}'
   return ${return_code}

the debugging shows:
+ return 0
my_called_script.ksh[183]: return_code not found
Clean verify, return_code = ${return_code}

I will check this tomorrow, heading out, thanks.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38738207
Omit the spaces around the equal sign in the variable assignment and it will work!

return_code=$?

And don't use single quotes with echo if you want to have the variable resolved:

 echo "Clean verify, return code = ${return_code}"
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 38738210
Ok, thanks, will try again tomorrow.
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 300 total points
ID: 38739541
Alternatively, you can have your 'called' script echo it's return code instead.

Like this:

# main script

  rc1=$(my_called_script1.ksh ${parm1} ${parm2})
  rc2=$(my_called_script2.ksh ${parm3} ${parm4})

  echo "Result of my_called_script1: ${rc1}"
  echo "Result of my_called_script2: ${rc2}"

# end main script

at the last line of my_called_script just echo your return string like OK, ERROR, 0 or 1 based in the (internal) result of each my_called_script.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 38740266
gerwinjansen - ok, checking this out now, thank you.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 38740728
Ok, looks like I got it working . . . here is the output:

+ return 16
my_script_1, return code = 16
my_script_2, the return code = 16

so it's passed back up twice and looks good on the output.

I'm trying to see if this will cause the CA-7 job scheduler to show an abend, will leave it open for 24 hours.

gerwinjansen - didn't know about calling the script like a function, good to know.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 38756550
sorry for the delay, this worked great, closing out now.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question