?
Solved

ksh - Passing a function return value to a variable

Posted on 2003-03-05
6
Medium Priority
?
13,016 Views
Last Modified: 2013-12-26
I'm a Windows programmer by trade......

I have 2 books that both explain functions. Neither show how to assign a return function value to a variable. Either externally or within the same script. All examples I have found work the same as a Windows "subroutine"....returning no value. I think I'm close but don't have the correct syntax. Here's the pseudocode.
********************************
function getChars {
      var1=$1
      var2=$(var1%.*)
      return var2
}
.....
var3=$(getChars abcdefg)
********************************
In my case the function and the "call" to it are all in the same script. Can someone help me with the "syntax".."tricks"..."do's and don't's" of Korn Shell functions (ie. "Functions 101")?
Thanks...
0
Comment
Question by:GeekMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8071966
you cannot return variable (by reference) in shell scripting, you just can return values (either integer or strings).
If you need to "return" more than one value, you need to use global variables, or pack the resuklts together in the function and parse it at the calling place.
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8072023
0
 
LVL 1

Expert Comment

by:skian
ID: 8072056
When used, a function should be seen like a normal
program ie, like normal programs, a function can only
return an "exit" value which is an _integer_.

But unlike normal programs, variables defined in
function stay "alive" after the function returns so
you can use $var2 after calling getChars :


function getChars
{
     var2=$1
}
getChars abcdefg
echo $var2

Stephane
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 4

Expert Comment

by:Vinit Kain
ID: 8072225
in ksh
you can pass the value to a function like

getfilename $username
getfilename()
{
filename=$username.txt
return $filename
}
0
 
LVL 48

Accepted Solution

by:
Tintin earned 200 total points
ID: 8075027
You can sorta do what you want by:

function getChars {
     var1=$1
     var2=$(var1%.*)
     echo "$var2"
}

var3=$(getChars abcdefg)

ie:  if the function output something, then you can capture it in much the same way as if you are calling an external script.

return is used for returning a status value.  So in the above example, if something failed, you'd return a non zero value, eg:


function getChars {
     var1=$1
     var2=$(var1%.*)
     echo "$var2"

     if [[ sometest ]]
     then
         return 0 # Succesful
     else
         return 1 # failed
     fi
}

var3=$(getChars abcdefg) || exit $?

or alternatively

var3=$(getChars abcdefg)

if [[ $? -ne 0 ]]
then
   echo "The function failed"
   exit 1
fi


0
 

Author Comment

by:GeekMan
ID: 8138133
Thanks for all the help....
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question