HP-UX - General

Re: Using Shift to Pass Parameters

 
SOLVED
Go to solution
Bill Hassell
Honored Contributor

Re: Using Shift to Pass Parameters

If you want to create a variable number of variables, you can use the eval command. Start by grabbing all the parameters (limit is 1,024 parameters, probably not an issue) into an array, then create and assign each variable in a loop:

#!/usr/bin/ksh
QTY=$#
set -A PARMS $@
COUNTER=0
while [ $COUNTER -lt $QTY ]
do
let VARNUM=$COUNTER+1
eval "var$VARNUM=${PARMS[$COUNTER]}"
let COUNTER=$COUNTER+1
done
set | grep ^var

The last statement (set) shows all the current variables. If you want the variable names to all have the same length, add the line: typeset -Z2 $VARNUM right after QTY=$#

This script will create as many variables as you supply on the command line:

myscript 1 2 3 4 5 6 7 8 9 0 11 22 33 44 555 6666 7777
var01=1
var02=2
var03=3
var04=4
var05=5
var06=6
var07=7
var08=8
var09=9
var10=0
var11=11
var12=22
var13=33
var14=44
var15=555
var16=6666
var17=7777


Bill Hassell, sysadmin
Gilbert Standen_1
Frequent Advisor

Re: Using Shift to Pass Parameters

Mr. Hassell,
An elegant solution which works great!
Gil
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Gilbert Standen_1
Frequent Advisor

Re: Using Shift to Pass Parameters

I took the parms method that Bill provided, and modified it to to pass (variable_name parameter) in ordered pairs and to set the variable to it's ordered pair parameter value. Here is that version:

#more parent_script_v2.ksh
newtest_v2.ksh FILE_LOC_1 /m02/oracle/scripts FILE_LOC_2 /m04/backups

#more newtest_v2.ksh
#!/usr/bin/ksh
QTY=$#
set -A PARMS $@
COUNTER=0
while [ $COUNTER -lt $QTY ]
do
export VARNAME=${PARMS[COUNTER]}
let VARNUM=$COUNTER+1
eval "${VARNAME}=${PARMS[$VARNUM]}"
let COUNTER=$COUNTER+2
echo $VARNAME
done
set | grep ^FILE
echo 'execute the commands now'
echo $FILE_LOC_1
echo $FILE_LOC_2

OUTPUT of parent_script_v2.ksh:

#parent_script_v2.ksh
FILE_LOC_1
FILE_LOC_2
FILE_LOC_1=/m02/oracle/scripts
FILE_LOC_2=/m04/backups
execute the commands now
/m02/oracle/scripts
/m04/backups

All help on this greatly appreciated!!
Gil

If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Weertman
Frequent Advisor

Re: Using Shift to Pass Parameters

Hello Gil,

I do not think it coming handy to use shift.
It is vulnerable for bugs when you use so
many parameters.

#!/usr/bin/sh
ii=0
for xx
do
((ii = ii + 1 ))
echo $xx $ii
# if [ $ii -eq 1 -a "$xx" = "bla" ]
# then
# bla bla ...
# fi
done

output:

test.sh 21 22 33 44 55 66 77 88 99 00 10 111 222 333
21 1
22 2
33 3
44 4
55 5
66 6
77 7
88 8
99 9
00 10
10 11
111 12
222 13
333 14

Regards Gregor
Gilbert Standen_1
Frequent Advisor

Re: Using Shift to Pass Parameters

Discovered that "export variable_name" does what is needed for my purposes also (can export a large number of variables).

Question:
Are there any cases in which passing of parameters at the time a subroutine is called on the command line must be used INSTEAD OF using "export", i.e cases where export will fail ? I tried calling a subroutine script with " &" at the end of the command and it still knew what the value of the variable was thanks to export.

When I asked the question, it hadn't occurred to me that there was any way other than including the commands at the command line when invoking the other script(s), so perhaps I should have phrased the question more generally.
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
A. Clay Stephenson
Acclaimed Contributor

Re: Using Shift to Pass Parameters

The export mechanism would tend to be more cumbersome. Also consider the case of recursive scripts and functions. Exporting would prove difficult but parameters would happily create instances as needed.
If it ain't broke, I can fix that.