• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

ksh - Passing a function return value to a variable

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
GeekMan
Asked:
GeekMan
  • 2
1 Solution
 
GeekManAuthor Commented:
This is a duplicate question please refer to first listing.
"WebMaster", please delete.
Thank you.
0
 
noviceUnixCommented:
Here's the syntax that you might want to use.

********************************
function getChars {
     var1=$1
     var2=$(var1%.*)
     return $var2  //put a $ sign when you need to get  
                   //the value
}
.....
Calling a function in Kornshell
getChars "abcdefg"
-------------------------------------
getChars "abcdefg"
var3=$? //var3 gets the return code value from the
        // getChars function...

Hope this helps to you...try this should work..
0
 
GeekManAuthor Commented:
Thanks for your help
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now