Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing named parameters to a K-shell script 1

Status
Not open for further replies.

IMAUser

Technical User
May 28, 2003
121
CH
Hi,

I guess it must be possible to pass in named parametrs, but I dont know how.
I know that you could read parameters by saying $1, $2, $3...etc., but the users want to pass in values in any order, but they will say like
$run_my_script.ksh PERCENT=10 NAME=JANE
OR
$run_my_script.ksh NAME=JOHN CITY=TIMBAKTOO

so within my script I need to be able to read the value for PERCENT, NAME and CITY if they are passed.

Anyone help me with this please.


Many Thanks
 
Hi,

you will have to parse the input yourself;
man getopt or man getopts might give you a few ideas.

The examples given there could be easier to adjust if you could persuade your users to a syntax like this:
$run_my_script.ksh -p 10 -n JANE
OR
$run_my_script.ksh -n JOHN -c TIMBAKTOO

hope this helps

 
Try looking at getops - but that is looking for
Code:
myscript -p <percent> -n <name> ....
Alternatively
Code:
for i in $@
do
  part1=$(expr $i : "\([^=]*\)*")
  part2=$(expr $i : "[^=]*=\(.*\)")
  case $part1 in
    NAME ) NAME=$part2;;
    CITY ) CITY=$part2;;
    ....
  esac
done
which is ugly but functional

Ceci n'est pas une signature
Columb Healy
 
or how about (assumes all params are given by name=value pair):

for i in $*
do
eval $i
done

echo $NAME
echo $PERCENT

HTH,

p5wizard
 
p5wizard

Thanks, that's a new one on me! Have a star.

Ceci n'est pas une signature
Columb Healy
 
You could have them call the script with the parameters first, like:

NAME=JOHN CITY=TIMBAKTOO run_my_script.ksh

If you can slip a 'set -o keyword' into your users' profiles, then environmental variables can be defined anywhere on the command line, not just before the command, so the calls you originally described would work.

- Rod


IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top