Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

ksh functions

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-06-2007
scriptingmani scriptingmani is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 2 July 2009, 9:18 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
ksh functions

Hi All,

just wanted to ask if functions created in a Korn shell script can be passed parameters and if so what is the syntax for creating a function with parameters?

thanks

Mani
Sponsored Links
    #2  
Old Unix and Linux 07-06-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 4 December 2016, 9:12 PM EST
Location: NM
Posts: 10,876
Thanks: 457
Thanked 987 Times in 917 Posts
Yes - example

Code:
myfunction()
{
    echo "$1 $2"
}

myfunction "This is parameter 1"  " Another parameter"
myfunction  32 64

You call the function with a list of parms, inside the function the parms are identified by $n where n is the number of the parameter.
Sponsored Links
    #3  
Old Unix and Linux 07-06-2007
scriptingmani scriptingmani is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 2 July 2009, 9:18 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for the speedy response Linux

could it also return a single value if i used a return statement i.e. return $x ?
I have tried it but doesn't seem to work.

Mani
    #4  
Old Unix and Linux 07-06-2007
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 7 November 2016, 6:31 AM EST
Location: Bangalore,India
Posts: 2,152
Thanks: 21
Thanked 159 Times in 150 Posts
Quote:
Originally Posted by scriptingmani View Post
thanks for the speedy response Linux

could it also return a single value if i used a return statement i.e. return $x ?
I have tried it but doesn't seem to work.

Mani
Return statement is used to return an exit value from a shell function to the calling script.

To return string use echo statement.

Code:
myfunction()
{
    echo "$1"
}

str=$( myfunction "This is parameter 1"  " Another parameter")
echo $str

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
== vs -eq and functions danieldcc UNIX for Dummies Questions & Answers 5 05-17-2011 12:25 AM
Functions AWK danietepa UNIX for Dummies Questions & Answers 2 12-02-2009 08:54 AM
Use of functions amitrajvarma Shell Programming and Scripting 1 11-23-2007 06:52 AM
functions in Raom Shell Programming and Scripting 6 07-21-2006 04:06 AM
Regarding functions sendhilmani Shell Programming and Scripting 2 03-24-2006 04:40 AM



All times are GMT -4. The time now is 10:33 AM.