Solved

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

Posted on 2013-01-02
10
668 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

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

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now