×
UNIX.COM Login
Username:
Password:  
Show Password






👤


Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Return Code to shell script

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-09-2018
usrrenny usrrenny is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 12 March 2018, 6:45 PM EDT
Posts: 24
Thanks: 10
Thanked 1 Time in 1 Post
Return Code to shell script

Hi, I need to send the return code from a script to the parent shell script.
But i am suppressing outputs while calling the child script.
Is there any way still to get the return code from the child script with suppress output.

Below is my script:
I am using :$ while calling return.sh because i want to suppress the output.But everytime its returning a 0(zero) as a success status. But i want 123(thats in the return.sh).Below is the script for parent & child script.



Code:
:$(./return.sh)
echo reply is "$?"

return.sh


Code:
exit 123

Sponsored Links
    #2  
Old 03-09-2018
ctac_ ctac_ is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 26 June 2018, 2:58 PM EDT
Posts: 23
Thanks: 3
Thanked 7 Times in 7 Posts
If you don(t want the output of return.sh
no need of :


Code:
 : [...]
             The null command.  Exit status is set to zero.

you can try this way


Code:
./return.sh >/dev/null;echo reply is $?

The Following User Says Thank You to ctac_ For This Useful Post:
usrrenny (03-12-2018)
Sponsored Links
    #3  
Old 03-09-2018
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 July 2018, 4:24 PM EDT
Location: Loughborough
Posts: 1,288
Thanks: 388
Thanked 352 Times in 277 Posts
Your 'NOP' : is just that a 'NOP' with a meaningless argument of $(./return.sh) so its RC will always be zero, '0'.


Code:
[Prompt]./return.sh 2>&1 > /dev/null
[Prompt]echo "$?"

This will suppress most stuff printed to the terminal window but allow the RC to pass through.
The Following User Says Thank You to wisecracker For This Useful Post:
usrrenny (03-12-2018)
    #4  
Old 03-12-2018
usrrenny usrrenny is offline
Registered User
 
Join Date: Jul 2014
Last Activity: 12 March 2018, 6:45 PM EDT
Posts: 24
Thanks: 10
Thanked 1 Time in 1 Post
Thanks all for replying.

I have to use :$(can't use any other code for suppress output).
Is there a way to get return code while using :$
Sponsored Links
    #5  
Old 03-12-2018
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 16 July 2018, 9:59 PM EDT
Location: San Jose, CA, USA
Posts: 11,406
Thanks: 649
Thanked 3,970 Times in 3,393 Posts
Quote:
Originally Posted by usrrenny View Post
Thanks all for replying.

I have to use :$(can't use any other code for suppress output).
Is there a way to get return code while using :$
NO!

As you have been told by ctac_ and by wisecracker, the exit code from the : command is zero. Always zero. No matter what comes on the line after the leading :, the exit code from that command line will be zero.

And, NO! you do not have to use the : command to suppress output from a command substitution. And, with what you have shown us there is no reason to use command substitution at all.

If we knew what you were trying to do (instead of how you think you need to do it), we might be able to make suggestions that would meet your needs. The suggestions ctac_ and wisecracker supplied seem to meet your requirements as they have been stated:
  1. run your script named return.sh,
  2. discard any output sent to standard output by return.sh, and
  3. print the exit code returned by return.sh.
Although the code suggested by ctac_ more correctly represents the output that would be produced by the : command with a command substitution that failed. The : command redirects output directed to standard output by a command substitution to /dev/null, but it does not discard output directed to standard error output by a command substitution like the code suggested by wisecracker does.
The Following User Says Thank You to Don Cragun For This Useful Post:
wisecracker (03-13-2018)
Sponsored Links
    #6  
Old 03-13-2018
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 July 2018, 4:24 PM EDT
Location: Loughborough
Posts: 1,288
Thanks: 388
Thanked 352 Times in 277 Posts
I will add to Don's reply how are you going to catch a strange error that completely exits your result.sh .
The code below will NEVER reach your exit 167 ...
For example try this, I have called it RC.sh:


Code:
#!/bin/sh
# RC.sh
echo 'test'
echo 'bad line"
exit 167

OSX 10.13.3, default bash terminal...


Code:
Last login: Tue Mar 13 12:18:13 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./RC.sh 2>&1 > /dev/null
./RC.sh: line 4: unexpected EOF while looking for matching `''
./RC.sh: line 6: syntax error: unexpected end of file
AMIGA:amiga~/Desktop/Code/Shell> echo "$?"
2
AMIGA:amiga~/Desktop/Code/Shell> _

Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to get return code from mutt if an address is invalid/undeliverable from Unix shell script jzuber Shell Programming and Scripting 3 12-28-2011 05:48 PM
Need to return fail or pass from shell script on the basis of pl/sql code execution alok1301 Shell Programming and Scripting 2 06-05-2009 03:48 AM
return code from script mpang_ Shell Programming and Scripting 2 10-23-2007 05:50 PM
Capture Oracle return code in shell script Vikas Sood Shell Programming and Scripting 1 05-22-2006 05:32 PM
Store return code of shell script in oracle table sveera Shell Programming and Scripting 3 05-04-2005 01:25 PM



All times are GMT -4. The time now is 10:02 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.