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: 1 August 2014, 9:58 PM EDT
Location: NM
Posts: 10,160
Thanks: 269
Thanked 782 Times in 732 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 offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 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-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 02:32 AM.