passinf parameters to functions

passinf parameters to functions

Post by Lech Nierod » Sat, 24 Jul 1999 04:00:00



Hi,

I'm rather new to shell programming...
Is there a way to pass parameters to functions (by value or reference) ?
All I've found are simple functions without parameters, like fnc()
{...;}.

Thanx

 
 
 

passinf parameters to functions

Post by Eric Amic » Sat, 24 Jul 1999 04:00:00



> I'm rather new to shell programming...
> Is there a way to pass parameters to functions (by value or reference) ?
> All I've found are simple functions without parameters, like fnc()
> {...;}.

It depends on the shell.  Some shells (ksh, for example) allow parameters
to functions, and you refer to them within the function body by $1, $2,
etc.

--
Eric Amick
Columbia, MD


 
 
 

passinf parameters to functions

Post by Rea » Sat, 24 Jul 1999 04:00:00


Lech Nieroda wrote;
# Hi,
#
# I'm rather new to shell programming...
# Is there a way to pass parameters to functions (by value or reference) ?
# All I've found are simple functions without parameters, like fnc()
# {...;}.

Sure, just treat them like scripts. Example;

   function Display
   {
      echo "${1}"
   }

   Display "this"

... or even

   Line=`Display "this"`      # using backticks
   echo "${Line}"

Cheers,
Real

 
 
 

1. passing parameter to shell function as in the normal unix commands (not positional parameter)

Hi,

I have a function defined in in .profile which accepts three
parameters.

function myfn
{
var1=$1
var1=$3
var1=$3

I am wondering, whether the function can be invkoded as

"myfun -c var1 -d var2 -e var3"

The reason, I would prefer this method, because I don't have to know
the order of the parameters and optionally I can ignore one or more of
the parameter and set the default value, inside the program if needed.

note: I know, it can be invoked as "myfun var1 var2 var3"
      I am using ksh under AIX 5.2

Thanks,
Prince.

2. 1542c and BT-958 at same time?

3. kernel function parameter - crash

4. Problem compiling kdesupport

5. function call with variable nb. of parameters

6. Want non-typical X window screen dimensions

7. passing parameters with spaces to shell function

8. Masquerade & Samba

9. Same function, different parameters

10. Signal Handler Function - Additional "code" parameter?

11. ksh function parameter passing

12. Passing Parameters to a (library) function

13. function, function...who's got the function?