Solved

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

Posted on 2013-01-02
10
687 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

728 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