LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How can I get the return value of a function in a shellscript? (http://www.linuxquestions.org/questions/programming-9/how-can-i-get-the-return-value-of-a-function-in-a-shellscript-566292/)

[KIA]aze 07-03-2007 09:24 AM

How can I get the return value of a function in a shellscript?
 
How can I get the return value of a function in a shellscript?
If I use `` or $(), I get everything that is printed when the function is called.
But I want what I put in the return() statement.

I would like to be able to do something like this for example:

Code:

#! /bin/bash
foo()
{
  echo "$0 says: hello $1"
  zed=42;
  return 1;
}

var=`foo`
echo $var
#and I would like to get "1" as output of the script

For normal programs, I can use "$?", but this doesn't seem to work here.

puffinman 07-03-2007 10:35 AM

Using backticks won't set $?, but running the function without them will. You can grab the output in other ways...

Code:

#! /bin/bash
foo()
{
  echo "$0 says: hello $1"
  zed=42
  return 1
}

echo "In backticks..."
var=`foo`
echo $var
echo "return value: " $?

echo "Alone..."
foo
echo "return value: " $?


[KIA]aze 07-03-2007 11:17 AM

No, that doesn't work.
It always shows 0, probably considering the function was executed successfully:
Code:

In backticks...
./foo.sh says: hello
return value:  0
Alone... foo
return value:  0

I want to use personalized return values like 512, 47, strings or whatever.
Well, I can always use normal variables since they seem to be kept the same everywhere in the script unlike C/C++.

P.S:
Considering your signature, maybe you might be able to solve this guy's problem:
Automatic checking integrity of a text file

I'd be interested too. :)
I only know basic grep, sed and awk command-line usage and I currently have no idea how to write a script doing this. Maybe it's easier with perl. ;)

puffinman 07-03-2007 11:21 AM

No, put foo on its own line, not part of the echo string. Check your code very carefully. My output is as follows:

Code:

In backticks...
./ccc says: hello
return value:  0
Alone...
./ccc says: hello
return value:  1


[KIA]aze 07-03-2007 12:49 PM

Oops, small error from copy/pasting.
But I still get zeros.

Code:

>cat foo.sh
#! /bin/bash
foo()
{
  echo "$0 says: hello $1"
  zed=42
  return 512
}

echo "In backticks..."
var=`foo`
echo $var
echo "return value: " $?

echo "Alone..."
foo
echo "return value: " $?

Code:

>./foo.sh
In backticks...
./foo.sh says: hello
return value:  0
Alone...
./foo.sh says: hello
return value:  0

Here's my bash version info (and update is not possible since I'm not admin here, but I don't think it's related to the version anyway):
Quote:

>bash --version
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
Are you using bash too or another shell?

puffinman 07-03-2007 02:46 PM

Don't use 512, it's wrapping. Use 1 or 2 or anything but large powers of 2!

dawkcid 07-03-2007 03:12 PM

Exit codes (a.k.a. return values) are defined to be in the range 0-255. As puffinman said, anything larger is ignored (you only get the low 8 bits).

[KIA]aze 07-03-2007 04:36 PM

Ok, thanks, it works now. :)
But return only takes numeric values apparently.
Quote:

EXIT STATUS
The value of the special parameter '?' shall be set to n, an unsigned decimal integer, or to the exit status of the last command executed if n is not specified. If the value of n is
greater than 255, the results are undefined. When return is executed in a trap action, the last command is considered to be the command that executed immediately preceding the trap
action.
I should read the man more often. :rolleyes:

[I even started reading the source code of true and false. 82 lines for true.c and 2 for false.c ^^.]

So there is no way to access the return value directly as in C?:
Code:

if(foo()) command;

bigearsbilly 07-05-2007 09:18 AM

yes,
return writes only to the $? variable
x=func() will not work.

remember too you must catch it immediately if you want to use it
maybe like:
Code:

func()
value=$?

even an echo between the two lines will overwrite the value


to be precise you can only return +ve integer values
from 0-255

so if you return 256 it becomes a 0


All times are GMT -5. The time now is 05:49 AM.