ksh functions | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

ksh functions

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 07-06-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 December 2014, 6:52 AM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 822 Times in 764 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 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

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 07-06-2007
anbu23's Avatar
anbu23 anbu23 is online now Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 22 December 2014, 9:23 AM EST
Location: Bangalore,India
Posts: 2,098
Thanks: 12
Thanked 133 Times in 128 Posts
Quote:
Originally Posted by scriptingmani View Post
thanks for the speedy response

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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:50 AM.