The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's manas6 UNIX for Dummies Questions & Answers 0 06-05-2008 03:44 AM
return code from script mpang_ Shell Programming and Scripting 2 10-23-2007 02:50 PM
asking about return code naamas03 Shell Programming and Scripting 3 08-28-2007 01:53 AM
Return Code of tar in AIX dupeng AIX 3 02-22-2004 08:05 PM
return code from oracle lesstjm Shell Programming and Scripting 3 01-29-2002 11:50 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Sep 2007
Posts: 53
how to get return code in one line

I know how to get the returning code of a function but wonder if I can combine the follwoing two lines into one:
e.g.:
#!/bin/shell
...
#line 1
MyFunction arg1 arg 2
#line 2
rec=$? #this will be evaluated later
....

like in c/c++, we'd write one line:
rec=MyFunction(arg1, arg 2)

Because I have many of those funciton calls in one script where all return codes need to be collected & evaluated later. It'd look nicer with one less line here.

Thanks in advance.

Last edited by bluemoon1; 09-13-2007 at 02:45 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
The one thing that is hard to do is make shell look nice.

The closest you can get is

Code:
if do_my_thing ....
then
       echo it worked
fi
but you don't get the exit code into a variable.
Reply With Quote
  #3 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Sep 2007
Posts: 53
very neat! I tested it & looks the if statement evaluates if the returning code is 0 or not, isn't it??

However, I failed to make it to check if it is non-zero- tried a few syntax with no luck... such as: if [ ! domything ], if [ domything -ne 0 ].... can you make it?;-)
Reply With Quote
  #4 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by bluemoon1 View Post
However, I failed to make it to check if it is non-zero- tried a few syntax with no luck... such as: if [ ! domything ], if [ domything -ne 0 ].... can you make it?;-)
No alas, I haven't. However some programs can reverse the return code, eg grep with the -v option.

There is a program called "true" and one called "false", there should be one called "not"....

Code:
#!/bin/sh
if $@
then 
    exit 1
fi
exit 0
then you could do

Code:
if not myprogram
then
.....
Reply With Quote
  #5 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Sep 2007
Posts: 53
now you lost me- what is $@ ?
Reply With Quote
  #6 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
all the variable arguments except $0, useful as $0 is normally the script name, I'm suggesting that the first CODE block be put in a file called "not".

Last edited by porter; 09-13-2007 at 05:07 PM.
Reply With Quote
  #7 (permalink)  
Old 09-13-2007
Registered User
 

Join Date: Sep 2007
Posts: 53
wow... thank you "Porter"- or whoever you really are;-)
I've been doing a program with this new language in last couple of weeks. Now I'm really interested in learning more! I want to be as good as you one day... oh well, just wishful thinking;-)
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0