LinuxQuestions.org
Help answer threads with 0 replies.
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-31-2007, 03:36 PM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
getting function return values in a Bourne script?


I'm unable to get the return value from a user defined function. The code below is essentially taken out of Robbins & Beebe's Classic Shell Scripting, but I'm not getting it to work. What gives?
Code:
$ cat test.sh
#!/bin/sh

func()
{
    return 0
}

v=$(func)
echo "return = '$v'"
$ ./test.sh
return = ''
$
Any insight would be appreciated.
 
Old 07-31-2007, 03:43 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
ITYM

Code:
#!/bin/sh

func()
{
    echo 0
}

v=$(func)
echo "return = '$v'"
HTH.
 
Old 07-31-2007, 03:50 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Alternatively:
Code:
$ cat test.sh
#!/bin/sh

func()
{
    return 0
}

func
v=$?
echo "return = '$v'"
 
Old 07-31-2007, 04:09 PM   #4
ocicat
Member
 
Registered: May 2007
Posts: 208

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by wjevans_7d1@yahoo.co
ITYM...
It's interesting echo behaves this way. This also implies that functions returning a value can't use echo statements as a debugging aide if called in this manner.

The following is taken straight out of Robbins & Beebe (with only minor changes...):
Code:
$ cat ./test.sh
#!/bin/sh

equal()
{
    case "$1" in
        "$2") return 0 ;;
    esac
    return 1   
}

if equal 'a' 'b' ; then
    echo 'a & b are equal'
else
    echo 'a & b are not equal'
$ ./test.sh
a & b are not equal
$
This led me to believe that the earlier code would work.

Editorial comment: shell programming seems to have lots of idiosyncrasies...
 
Old 07-31-2007, 04:45 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Quote:
Originally Posted by ocicat
This led me to believe that the earlier code would work.
If you mean the first code you posted, then it doesn't works because you pick the standard output of the function instead of its return value.
The sample code I previously wrote is working as you expect.
 
Old 08-01-2007, 08:01 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
It's evident from the book's example that you want jlliagre's solution, not mine. The following details may help, though.

Whenever you call a function in your shell script, it's as though you were calling a separate shell script. The function you call, just like any shell script, has a return code and can do output to standard output and standard error.

Both jlliagre's answer and the book use the return code. This is excellent for making decisions, which is the way the book uses it.

If you ever want a more complicated return value, though, use standard output. "More complicated" can mean alphanumeric strings, for example. The return code is limited to integers between 0 and 255.

And if you want to debug your function, send debugging data to standard error, not standard output:

Code:
echo "got to this point in the code" 1>&2
 
  


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
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 06:54 PM
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
system() and return values PatrickNew Programming 1 02-07-2007 05:35 AM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 10:52 AM


All times are GMT -5. The time now is 12:36 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration