LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-09-2009, 09:39 AM   #1
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Rep: Reputation: 16
Get return value of function


Hi, Just want to confirm code below, so the third option is the only way of getting the return value of a function in shell scripts? ( excluding modifying parameter values )

Code:
[morton@mortonlx scr]$ cat -n myreturn.sh 
     1  #! /bin/sh
     2  returnthis()
     3  {
     4          return 1;
     5  }
     6
     7  myval=`returnthis`
     8  echo "backticks:  $myval"
     9
    10  myval=$(returnthis)
    11  echo "\$() : $myval"
    12
    13  returnthis
    14  echo "\$? : $?"
    15
[morton@mortonlx scr]$ ./myreturn.sh 
backticks:  
$() : 
$? : 1
Thanks =)
 
Old 10-09-2009, 09:46 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Code:
#!/bin/sh
echothis()
{
   echo "This is what you get with backticks"
   return 1
}

myval=`echothis`
echo "backticks:  $myval"

myval=$(echothis)
echo "\$() : $myval"

echothis
echo "\$? : $?"
 
Old 10-09-2009, 10:57 AM   #3
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
So you mean what I said is true?
 
Old 10-09-2009, 12:08 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by mortonmorton View Post
So you mean what I said is true?
It is. The value set by a function when it calls return is available to the caller as $? I've never called a function in ` ` or $( ) but expect them to be functionally equivalent in this case ($( ) is normally preferred), both being replaced with whatever the called function wrote to stdout. In case whatever is called in ` ` or $( ) writes something to stdout that includes whitespace characters it is safer to enclose them in double quotes.
 
Old 10-09-2009, 10:54 PM   #5
mortonmorton
Member
 
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
Thanks! I guess I got used to other prog. languages where you could directly assign the function to a variable =)
 
  


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
where is return value of a function stored? fssengg Programming 7 06-26-2008 02:43 AM
return value of Clock function namanhams Programming 1 04-01-2008 01:00 PM
How to get return value from C function using shell yhacks Programming 3 03-11-2008 05:58 PM
How can I get the return value of a function in a shellscript? [KIA]aze Programming 8 07-05-2007 09:18 AM
using return in recursive function hubabuba Programming 9 10-10-2005 10:59 AM


All times are GMT -5. The time now is 06:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration