LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 08: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 08:28 AM.
 
Old 07-03-2007, 09:35 AM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
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, 10: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 10:19 AM.
 
Old 07-03-2007, 10:21 AM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
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 10:22 AM.
 
Old 07-03-2007, 11:49 AM   #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 11:56 AM.
 
Old 07-03-2007, 01:46 PM   #6
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
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 01:48 PM.
 
Old 07-03-2007, 02: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, 03: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, 08:18 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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



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 11:47 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 01:05 PM
getting the return value from a thread start function cynthia_thomas Programming 1 02-18-2006 08:23 AM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:12 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration