- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Using Shift to Pass Parameters
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 03:14 AM
01-25-2005 03:14 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 03:22 AM
01-25-2005 03:22 AM
Re: Using Shift to Pass Parameters
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 03:25 AM
01-25-2005 03:25 AM
Re: Using Shift to Pass Parameters
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 03:26 AM
01-25-2005 03:26 AM
Solutionyou 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 03:29 AM
01-25-2005 03:29 AM
Re: Using Shift to Pass Parameters
Therefore in base64 you could use
1 - 10
a - z
A-Z
@ and _
for your 64 possible shift values.
HTH,
Jeff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 03:29 AM
01-25-2005 03:29 AM
Re: Using Shift to Pass Parameters
echo ${10}
For those above 9.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 04:37 AM
01-25-2005 04:37 AM
Re: Using Shift to Pass Parameters
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 05:43 AM
01-25-2005 05:43 AM
Re: Using Shift to Pass Parameters
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 06:07 AM
01-25-2005 06:07 AM
Re: Using Shift to Pass Parameters
Oops I see now that the name is "Godron" and not "Gordon". Please accept my apology.
Gil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 06:19 AM
01-25-2005 06:19 AM
Re: Using Shift to Pass Parameters
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 06:30 AM
01-25-2005 06:30 AM
Re: Using Shift to Pass Parameters
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 08:53 AM
01-25-2005 08:53 AM
Re: Using Shift to Pass Parameters
An elegant solution which works great!
Gil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 10:01 AM
01-25-2005 10:01 AM
Re: Using Shift to Pass Parameters
#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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2005 10:12 PM
01-25-2005 10:12 PM
Re: Using Shift to Pass Parameters
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 07:38 AM
01-26-2005 07:38 AM
Re: Using Shift to Pass Parameters
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2005 07:45 AM
01-26-2005 07:45 AM