Link to home
Start Free TrialLog in
Avatar of GuntherVonHagens
GuntherVonHagensFlag for Germany

asked on

KSH What's the best way to pass multiple variables (some may be blank) to a function?

In korn shell scripting, what's the best way to pass multiple variables (some of which may be blank) to a function ... ensuring the correct local variables are updated?

i.e. if I pass 3 variables var1, var2 and var3 into a function but var2 is blank I don't want the value of $2 to equal var3.
Of course  I can pass a dummy value for all blanks. Or I can use 'shift'. What's the best way? Thanks.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

you can try reserving the position and passing no value using "" e.g

myshell 1 "" 5

$2 will be null
Or use explicit variable names and parse them in the function, like this:

YourFunction var1=val1 var3=val3
@gerwinjansen I don't think that assigning variables like that works.  If you swap it round so that the function name is at the end of the line, you have effectively set the $var1 and $var2 variables in the main script to "val1" and "val2", then used those variables in the function call.

I would use the @omarfarid solution, but would quote all of the parameters, since you need to protect empty variables too:

    myfunction "$var1" "$othervar" "$name1" "$street"

That way, the value of "$street",for example, would always be $4 within the function (also protects against problems caused by values having spaces in them).
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GuntherVonHagens

ASKER

Thanks. That's what I was looking for ... I didn't want to pass in a dummy value when a variable is blank. Nice one.