Getopts option -please help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getopts option -please help
# 1  
Old 12-17-2013
Getopts option -please help

Hello,

I am using below code in AIX env to interpret -n option given in argument while executing the script .I want to give another argument -t
Code:
#!/bin/sh
#set -x
while getopts ":n:" opt; do
    case "$opt" in
        n)
            host=$OPTARG
            shift 2
        ;;
        *)
            break
        ;;
    esac
done

echo "host = $host "


I want to run script like ./ser_reloc.sh -n dev-u1 -t pref

How do I recognize second argument -t in the script
Additionally,if I place -t before -n and use script like below:

Code:
./ser_reloc.sh  -t  perf -n dev-u1

would I have to change code for this?


Best regards,
Vishal
# 2  
Old 12-17-2013
Use
Code:
#!/bin/sh
#set -x
while getopts ":n:t:" opt; do
    case "$opt" in
        n)
            host=$OPTARG
        ;;
        t)
            targ=$OPTARG
        ;;
        *)
            break
        ;;
    esac
done

echo "host = $host targ = $targ"

Do not use shift, because getopts is already taking care of removing option-arguments.
Code:
$ ./script.sh -n abc -t def
host = abc targ = def

This User Gave Thanks to hergp For This Post:
# 3  
Old 12-17-2013
I also want to send the user an error message if the user doesn't input the
-n and -t correctly

-n is node name so I would ping this and if ping is working then its fine else an error message needs to be displayed to user that please input a valid host name .Similarly I want the user to input -t as pref or avail .If that doesn't happen then it should error out saying user that please input a valid value



Best regards,
Vishal
# 4  
Old 12-17-2013
After the while...done loop you could write
Code:
if ping -w 2 $host >/dev/null 2>&1
then
    echo Host OK
else
    echo Bad Host $host
fi

and
Code:
case "$targ" in
    pref|avail) echo targ OK;;
    *)    echo Bad argument to -t;;
esac

This User Gave Thanks to hergp For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Unrecognized option: sparc-sun-Solaris2.10/bin/as: unrecognized option `-m32'

Hi, I installed some packages required by an app built with python. But when I try python setup.py install, I get the following error: /opt/csw/lib/gcc/sparc-sun-solaris2.10/5.2.0/../../../../sparc-sun-solaris2.10/bin/as: unrecognized option `-m32' Could anyone tell me what's wrong... (4 Replies)
Discussion started by: Kimkun
4 Replies

2. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

3. Shell Programming and Scripting

ksh "getopts" -- Unsetting an Option

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its... (4 Replies)
Discussion started by: Matt Miller
4 Replies

4. Shell Programming and Scripting

How to execute default in getopts when no option is given ?

hi, here is a ksh script i wrote using getopts... i want to find out how i can run it in default mode when no option is mentioned and no arguments are provided... ? i.e if the script name is final1, then just running final1 should run in default mode.... while getopts 1:2:3:4: mode ... (1 Reply)
Discussion started by: pravsripad
1 Replies

5. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

6. Shell Programming and Scripting

getopts fails to error on option w/o dash

I have a script with several options and during testing I found that the \? option does not handle options without dashes as I would expect. Then I run the script with any option that does not include a dash, it runs the script when I would expect \? to catch it and error. I've tried this with... (2 Replies)
Discussion started by: HexKnot
2 Replies

7. Shell Programming and Scripting

getopts with repeat of same option

Hello, Does getopts have some way of handling the use of an option that requires a parameter more than once on the command line. e.g. mycmd -a john -a jane I came up with a solution using arrays (shown below), but wonder if getopts has some other way of handling it. Other solutions... (2 Replies)
Discussion started by: CarlosNC
2 Replies

8. Shell Programming and Scripting

getopts: bad option(s)

Hi, I have a script that ran perfectly on Solaris 5.8 However after upgrade to Solaris 5.10 it started failing. I invoke the script as below: ./TestScript3.ksh --dir $APP_DATA_IN_OLD $NDM_DATA/$NEXT_FILE When i execute it i get the following error "getopts: dir bad option(s)". Please let... (1 Reply)
Discussion started by: JoeJoseph
1 Replies

9. Shell Programming and Scripting

getopts with non-option arguments?

Hello everyone, Is it possible to use getopts and also receive arguments without option flags? e.g. myscript arg1 arg2 -a arg3 -b arg4 If so, how do you stop getopts from exiting as soon as it detects the non-option arguments? (2 Replies)
Discussion started by: kdelok
2 Replies

10. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies
Login or Register to Ask a Question