LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2007, 09:24 AM   #1
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
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.

Last edited by [KIA]aze; 07-03-2007 at 09:28 AM.
 
Old 07-03-2007, 10:35 AM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
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: " $?
 
Old 07-03-2007, 11:17 AM   #3
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
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.

Last edited by [KIA]aze; 07-03-2007 at 11:19 AM.
 
Old 07-03-2007, 11:21 AM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
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

Last edited by puffinman; 07-03-2007 at 11:22 AM.
 
Old 07-03-2007, 12:49 PM   #5
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
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?

Last edited by [KIA]aze; 07-03-2007 at 12:56 PM.
 
Old 07-03-2007, 02:46 PM   #6
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

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

Last edited by puffinman; 07-03-2007 at 02:48 PM.
 
Old 07-03-2007, 03:12 PM   #7
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
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).
 
Old 07-03-2007, 04:36 PM   #8
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
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.

[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;
 
Old 07-05-2007, 09:18 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sh script: problem with return value in function jhwilliams Linux - Software 1 06-20-2007 12:47 PM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 02:05 PM
getting the return value from a thread start function cynthia_thomas Programming 1 02-18-2006 09:23 AM
using return in recursive function hubabuba Programming 9 10-10-2005 10:59 AM
can a function return a string? hubabuba Programming 13 03-06-2005 03:51 PM


All times are GMT -5. The time now is 06:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration