how to get return code in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get return code in one line
# 1  
Old 09-13-2007
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 06:45 PM..
# 2  
Old 09-13-2007
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.
# 3  
Old 09-13-2007
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?;-)
# 4  
Old 09-13-2007
Quote:
Originally Posted by bluemoon1
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
.....

# 5  
Old 09-13-2007
now you lost me- what is $@ ?
# 6  
Old 09-13-2007
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 09:07 PM..
# 7  
Old 09-13-2007
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;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Displaying command return in one line

Hello all I have a query (SQL) that returns a rather long field from an Oracle database. The field in question is defined on 400 characters but all these 400 cannot be displayed by the echo command. Thus when I launch the following command: echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s... (9 Replies)
Discussion started by: S. BASU
9 Replies

2. Shell Programming and Scripting

Return line when line below has the string

I have a file with data like this. I want to look for MultiLoad and pull that line and the line above. I cant use the grep -B 1 I dont have that version and cant install on the server. is there a awk or perl that will do it. "CMPGN_RPT_DEV_TT"."text1" (TableId 0029H 5CEEH) bypassed due to... (4 Replies)
Discussion started by: wambli
4 Replies

3. Shell Programming and Scripting

Need help in search for word and return line

Hi, I have this file format and can't seem to think of a solution. I need your help. I want to return lines which says "Record" if it's ID > 0 file.txt Record 1: :::::::::::::::::::::: :::::::::::::::::::::: ID "000001" :::::::::::::::::::::: :::::::::::::::::::::: ... (6 Replies)
Discussion started by: jakSun8
6 Replies

4. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

5. Shell Programming and Scripting

Need a carriage return at end of each line

Hi All, I am reading two files and writing out the file name and count of lines in each file to an output file. My script looks like this: echo "input_file1.out;`wc -l < input_file1.out | sed 's/^]*\(.*\)]*$/\1/'` " > comp_file1.out echo "input_file2.out;`wc -l < input_file2.out | sed... (2 Replies)
Discussion started by: Hangman2
2 Replies

6. Shell Programming and Scripting

grep return more than one line

Hi, I have a lot of log files which contain lines in the format of (date info) or (info), and when I use grep to search for "date" I was able to get the (date info) line, but some info lines are a separate line after the (date info) line... for example like: (date info info info) (date... (8 Replies)
Discussion started by: a27wang
8 Replies

7. Shell Programming and Scripting

How to get the return code of subroutines executed as standalone as command line in Perl ?

How to do I get the return code of a subroutine in a perl module if invoke the subroutine as standalone, I have an module say TestExit.pm and in that i have a subroutine say myTest() which is returns 12, if i were to call the subroutine from command line like CASE:1 ( Without an explict... (2 Replies)
Discussion started by: ennstate
2 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

return the previous line

Hello friends , I am doing the following command, but it is not wise to all files. for temp in `find ./CSV/ -name "*.txt"` do sed -n -e 'N; /*Main End/p' $temp done Its give me the correct output for some files , but not for all files. I mean some files contains the... (12 Replies)
Discussion started by: user_prady
12 Replies

10. UNIX for Dummies Questions & Answers

grep for a line then return lines above

Hey guys, I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out? This is on Solaris 9..... I don't have GNU grep so -B and -A commands will not work (3 Replies)
Discussion started by: kingdbag
3 Replies
Login or Register to Ask a Question