Passing options into a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing options into a script
# 1  
Old 05-20-2010
Passing options into a script

Afternoon all,

I have been writing a script to do some selects on a table dependent on what options are selected when the script is run:

Code:
#!/bin/ksh

set -x
set -m

if [ "$1" = "" ]
then
   echo "usage: msglog.ksh -da <date and time> -i <interface> -m <msg> -di <direction> -mi <MIR>"
   exit 1
fi
while getopts "da:i:m:di:s:mi:" opt;do
    case $opt in
         da ) dtandtm="$OPTARG";;
         i ) interface="$OPTARG";;
         m ) msg="$OPTARG";;
         di ) direction="$OPTARG";;
         mi ) MIR="$OPTARG";;
    esac
done

Unfortunately it appears that if you use more than one flag the the second option is just passed to the first variable instead of creating a second.

Output of debug:

./msglogcount.ksh -i XCLR -di O
+ set -m
+ [ -i = ]
+ getopts da:i:m:di:s:mi: opt
+ interface=XCLR
+ getopts da:i:m:di:s:mi: opt
+ getopts da:i:m:di:s:mi: opt
+ interface=O
+ getopts da:i:m:di:s:mi: opt


As you can see instead of direction being set to O, it just overwrites the interface variable.

Any ideas?

Thanks

Chris
# 2  
Old 05-20-2010
Hi.

A quote from the getopts man page:
Quote:
An option begins with a + (plus sign) or a - (minus sign) followed by a character. An option that does not begin with either a + or
a - ends the OptionString.
That's to say that da, dm and mi would be counted as six options, not three.

If you can change your script, i.e.:
Code:
#!/bin/ksh

set -x
set -m

if [ "$1" = "" ]
then
   echo "usage: msglog.ksh -da <date and time> -i <interface> -m <msg> -di <direction> -mi <MIR>"
   exit 1
fi
while getopts "A:i:m:D:s:M:" opt;do
    case $opt in
         A ) dtandtm="$OPTARG";;
         i ) interface="$OPTARG";;
         m ) msg="$OPTARG";;
         D ) direction="$OPTARG";;
         M ) MIR="$OPTARG";;
    esac
done

then call it as
Code:
./msglogcount.ksh -i XCLR -D O

then you should have no problems
# 3  
Old 05-20-2010
That's because getopts uses only single-character options. Anything other than a colon and the question mark are interpreted as a single option character.
# 4  
Old 05-20-2010
Quote:
Originally Posted by chris01010
Code:
getopts "da:i:m:di:s:mi:" opt

You should probably read the man page of "getopts" more thoroughly. Unix options are considered to be 1-character only ("-da" is considered to be equivalent to "-d -a" and "-a -d") and getopts implicitly adheres to this convention.

Therefore the option string "da:i:m:di:s:mi:" means: there is an option "-d", which gets no parameter at all, an option "-a", which expects a parameter, an option "-i", which also expects an parameter, ....

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to iterate over several options

Have two 3 files which has list of servers,users and location and base url which is common on every server A = server1 server2 server3 B = user1 user2 user3 C = dom1 dom2 dom3 baseurl=/opt/SP/ and what i have to achieve is below via ssh from REMOTE SERVER for it's first iteration it... (7 Replies)
Discussion started by: abhaydas
7 Replies

2. Shell Programming and Scripting

Passing new options to a running command

I was just wondering if there is a way to pass an new option or argument to an already running command or program. Let us say you run "zenity --list <options>" and you like to add a title or change the title you used on it while it is already running, is it possible to do that? (1 Reply)
Discussion started by: cryogrid
1 Replies

3. Shell Programming and Scripting

shell script options

one thing i was trying to figure out is if you can give people the option to choose what they want to do in a shell script. for example, let's just say that you have a simple shell script to install a couple of programs, can you make it to where you can press a certain key to install a certain... (1 Reply)
Discussion started by: hotshot247
1 Replies

4. UNIX for Dummies Questions & Answers

[solved] Script creation (how to include options in the script)

Hi guys i have written a script which takes the options given to him and execute itself accordingly. for example if a script name is doctortux then executing doctortux without option should made doctortux to be executed in automatic mode i.e. doctortux -a or if a doctortux is needed to run in... (4 Replies)
Discussion started by: pinga123
4 Replies

5. Shell Programming and Scripting

Help with options and arguments to a script

I'm trying to write a script that accepts both arguments and options, e.g. ./script -h 1 -m 15 -s 30 or ./script -h 1 -m 15 -s 30 I'd like for any of the arguments and options to be optional, and the option values should be numerals only. I've tried both getopt and getopts but I... (1 Reply)
Discussion started by: Ilja
1 Replies

6. Solaris

How to run a script with options

I have a script name as psin_install_i3fp.sh I need to run this script like ./psin_install_i3fp.sh step2 What this step2 represents? my script contains data: #!/bin/ksh mkdir logs >> /dev/null 2>&1 ./infra/bin/psin_stop_ba.sh mv ./psin_start_ba.sh... (2 Replies)
Discussion started by: pmrajesh21
2 Replies

7. Shell Programming and Scripting

Adding options to a shell script

I want to add options to my shell script but having problems, my code so far is; #!/bin/bash lflag= iflag= while getopts 'l:i:' OPTION do case $OPTION in l) lflag=1 lval="$OPTARG" ;;... (1 Reply)
Discussion started by: paulobrad
1 Replies

8. Shell Programming and Scripting

Passing options to a bash script

I'm just not sure where to start looking into this. I want to be able to create switches for my script. Back in the day I'd make my scripts interactive...you know: echo "what report do you want" echo "A)boxes with errors" echo "B)boxes with more than 5 errors" echo "C)Service groups that have... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

9. Shell Programming and Scripting

Handeling multiple options in script

i need to be able to handel if multiple commands are passed into my script. (ie) -f -r -i . does anyone know a simple solution to handeling this. Thanks (2 Replies)
Discussion started by: virus_stinger
2 Replies
Login or Register to Ask a Question