Operating System - HP-UX
1834454 Members
2945 Online
110067 Solutions
New Discussion

Re: Using Shift to Pass Parameters

 
SOLVED
Go to solution
Gilbert Standen_1
Frequent Advisor

Using Shift to Pass Parameters

Hi,

I have a shell script which calls another script:

write_backup_script_v12.ksh $1 $2 $3 $4 $5 $6 $7 $8 $9

As you can see, script passes 9 parameters. Want to pass more than 9 parameters. How can I pass additional parameters beyond $9 ?

I've done research on "shift" but so far I don't see how to use shift to solve the problem. Are there other ways besides "shift" ? If not, could you give an example of how shift would be used in this case ?

It's appreciated!

Gil
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
15 REPLIES 15
Steven E. Protter
Exalted Contributor

Re: Using Shift to Pass Parameters

Here is an example of how to use shift.


numarg=$#
loop=$numarg+1
control=5
while (( control != $loop ))
do
subject = "${subject} ${5}"
shift
done


What this loop does is take all parameters after the fifth parameter and uses it to create the multi word subject for an email.

It demonstrates the programming principle involved.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jeff Schussele
Honored Contributor

Re: Using Shift to Pass Parameters

Hi Gilbert,

Yes, but not in base 10.
If you change the base with the baseXX command - EX base16 - then you could shift a or shift d to get past 10 shifts.
Note the max base is base64.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Peter Godron
Honored Contributor
Solution

Re: Using Shift to Pass Parameters

Gil,
you could use a temporary file to write/read the parameters.
Or pass the all the parameters enclosed by one set of double quotes.
write_backup_script_v12.ksh "$1 $2 ...$x" then use awk or cut to assign the parameters within the program.

Regards
Jeff Schussele
Honored Contributor

Re: Using Shift to Pass Parameters

I guess I should have highlighted the fact that the shift value can *only* be 1 character.
Therefore in base64 you could use
1 - 10
a - z
A-Z
@ and _
for your 64 possible shift values.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Rodney Hills
Honored Contributor

Re: Using Shift to Pass Parameters

Of you can use the format-
echo ${10}

For those above 9.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Using Shift to Pass Parameters

Another option under ksh is using an array-
set -A p "$@"

within your script and this will assign array p as follows-
${p[0]} will be the first param
${p[1]} will be the second param
...
${p[12]} will be the thirteenth param
etc
etc

HTH

-- Rod Hills
There be dragons...
Gilbert Standen_1
Frequent Advisor

Re: Using Shift to Pass Parameters

Hi,

These are all excellent replies and are very much appreciated.

Peter Gordon's solutions were for me the most readily accessible, and since I'm a DBA and not a sysadmin, am taking the first one I can implement.

Somehow it's still not clear to me how shift is used to solve this.

so here is my code, generalizable to i parameters (is there an upper limit on i using the code below ?)

SCRIPT test.ksh (calls test_shift.ksh)

test_shift.ksh "100 jim joe frank al mike gil susan kenny jane phil"

SCRIPT test_shift.ksh (sets the values)

echo $1
let i=1
let j=1
while [ "$i" -lt 12 ]
do
#echo $1 | cut -f"$i" -d' '
echo $1 | cut -f"$i" -d' ' | read parval
if [ "$i" -eq 1 ]
then
export var1=$parval
echo 'var1='$var1
elif [ "$i" -eq 2 ]
then
export var2=$parval
echo 'var2='$var2
elif [ "$i" -eq 10 ]
then
export var10=$parval
echo 'var10='$var10
elif [ "$i" -eq 11 ]
then
export var11=$parval
echo 'var11='$var11
fi
let i=$i+1
done

echo 'now execute the script commands'

OUTPUT of scripts:

# test.ksh
100 jim joe frank al mike gil susan kenny jane phil
var1=100
var2=jim
var10=jane
var11=phil
now execute the script commands




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

Peter,
Oops I see now that the name is "Godron" and not "Gordon". Please accept my apology.
Gil
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
john korterman
Honored Contributor

Re: Using Shift to Pass Parameters

Hi,
this is a simple example of how to use the shift command:

#!/usr/bin/sh
typeset -i NUM_ARGS=0
NUM_ARGS="$#"
while [ "$NUM_ARGS" -gt 0 ]
do
echo "$1"
shift
NUM_ARGS=$(($NUM_ARGS - 1 ))
done

end of script args.sh...
Try executing args.sh like this:

# args.sh "100" "jim" "john joe" "" "frank" "al" "mike o,brian" "gilberto gil" "susan" "kenny" "jane" "phil"

in this way the script can handle more words as one parameter and even empty parameters.

regards,
John K.
it would be nice if you always got a second chance
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.