Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

ksh functions

Shell Programming and Scripting


Closed Linux or Unix Question    
 
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: 27 May 2015, 12:00 AM EDT
Location: NM
Posts: 10,453
Thanks: 336
Thanked 860 Times in 799 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: 16 March 2015, 4:56 AM EDT
Location: Bangalore,India
Posts: 2,136
Thanks: 14
Thanked 150 Times in 143 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 Linux or Unix Question

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-16-2011 11:25 PM
Functions AWK danietepa UNIX for Dummies Questions & Answers 2 12-02-2009 07:54 AM
Use of functions amitrajvarma Shell Programming and Scripting 1 11-23-2007 05:52 AM
functions in Raom Shell Programming and Scripting 6 07-21-2006 03:06 AM
Regarding functions sendhilmani Shell Programming and Scripting 2 03-24-2006 03:40 AM



All times are GMT -4. The time now is 01:47 AM.