Return Code to shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return Code to shell script
# 1  
Old 03-09-2018
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

# 2  
Old 03-09-2018
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 $?

This User Gave Thanks to ctac_ For This Post:
# 3  
Old 03-09-2018
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.
This User Gave Thanks to wisecracker For This Post:
# 4  
Old 03-12-2018
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 :$
# 5  
Old 03-12-2018
Quote:
Originally Posted by usrrenny
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.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-13-2018
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> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Email shell script return code

Hi All, I'm new to unix and I have a requirement to execute a shell script and get the return code/Exit code and send it in email. Meaninig we execute a script which in turn execute a shel script and fetches it\s return code and send it to email. I tried the code below as : ... (2 Replies)
Discussion started by: midhun3108
2 Replies

2. Shell Programming and Scripting

How to capture script return code?

Hi I am executing database backup via shell script (Korn). The backup log is long, but I would like to capture only the last line so I can send an email if it fails Example of failed backup (only last 3 lines) BR0056I End of database backup: bejbofoh.aff 2012-07-26 07.31.21 BR0280I... (7 Replies)
Discussion started by: nimo
7 Replies

3. Shell Programming and Scripting

Need to get return code from mutt if an address is invalid/undeliverable from Unix shell script

I am using mutt on ksh Unix to send emails to addresses plucked from the database. If the "To:" email address is not longer valid and so the email is not sent to the "To:" recipient, but is sent to the valid cc address, I need to be able to get an error code returned to the shell script so that... (3 Replies)
Discussion started by: jzuber
3 Replies

4. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies

5. Shell Programming and Scripting

Need to return fail or pass from shell script on the basis of pl/sql code execution

Hi guys, I am quite new in shell scripting. I am tring to promote some oracle jobs into control-M. In control-M, I am calling a script which establishes a connection with database and execute some procedures. Now I want if that PL/sql Block got failed script should return failure to... (2 Replies)
Discussion started by: alok1301
2 Replies

6. Shell Programming and Scripting

How to return the value from the called shell script to the calling sh script

Hi all, I have two ksh scripts #sample1.sh #!/bin/ksh . ./sample2.sh echo $fileExist #sample2.sh #!/bin/ksh func() { i=1 return $a } func echo $? Here how should I return the value of sample2.sh back to sample1.sh? Thanks in advance. (2 Replies)
Discussion started by: gp_singh
2 Replies

7. Shell Programming and Scripting

How to return value in shell script

im using bourne shell In file scriptA i add a new function, test_return() test_return() { a=1 return $a } when i try execute , a='/''/scriptA test_return echo $a its give me error.. scriptA: cannot return when not in function ist any solution for this problem.. How... (1 Reply)
Discussion started by: neruppu
1 Replies

8. Shell Programming and Scripting

return code from script

hey all, I have a simple script #!/bin/bash ./cron.sh test_a.txt & ./cron.sh test_b.txt & wait I want to invoke another program based on the return status of the above calls. How am I were to get that? Thanks! (2 Replies)
Discussion started by: mpang_
2 Replies

9. Shell Programming and Scripting

Capture Oracle return code in shell script

I am using the following code in my shell script list=`sqlplus -s $user/$pwd@$dbms<<EOF WHENEVER SQLERROR EXIT SQL.SQLCODE set pagesize 0 feedback off verify off heading off echo off select * from control_tbl where src_nm=$3 and extrct_nm=$4; exit SQL.SQLCODE; EOF` ERROR=$?... (1 Reply)
Discussion started by: Vikas Sood
1 Replies

10. Shell Programming and Scripting

Store return code of shell script in oracle table

Hi friends, I have to do the following things : 1) there should be a shell script returning the returning the return code of the script. and i have to add some more details like on which machine is has run , at what time and other details and then using plsql i have to add a row to Oracle... (3 Replies)
Discussion started by: sveera
3 Replies
Login or Register to Ask a Question

Featured Tech Videos