Solved

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

Posted on 2013-01-02
10
681 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
  • 6
  • 3
10 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 175 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 175 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 75 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

820 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