Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-02
10
Medium Priority
?
695 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
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…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

972 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