- 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
Discussions
Discussions
Forums
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
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 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
Re: Using Shift to Pass Parameters
- « Previous
-
- 1
- 2
- Next »