Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ksh - Passing a function return value to a variable

Posted on 2003-03-05
6
Medium Priority
?
13,616 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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

564 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