Why getopts doesn't detect correctly my switches?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why getopts doesn't detect correctly my switches?
# 1  
Old 03-13-2009
Question Why getopts doesn't detect correctly my switches?

Dear all,

I have created a KornShell script containing swiches with getopts (command line switches). Normally, my script should work like this:

Code:
$ ./myscript.ksh -a 12 -b 4 -c 78
The switch a was selected with the argument 12
The switch b was selected with the argument 4
The switch c was selected with the argument 78
$ 
$ 
$ ./myscript.ksh -a 12 -b 4 -c
The switch a was selected with the argument 12
The switch b was selected with the argument 4
CURRENT_SWITCH = c : error : missing argument

Now the problem is that when I don't supply an argument for the switch -c, the script
considers the precedent switch as the current switch that is CURRENT_SWITCH=b
instead of CURRENT_SWITCH=c. So here is what I obtain:

Code:
$ ./myscript.ksh -a 12 -b 4 -c
The switch a was selected with the argument 12
The switch b was selected with the argument 4
CURRENT_SWITCH = b : error : missing argument

Could somebody kindly guide me where did I make a mistake in my implementation?
Here is the script code:
Code:
#!/bin/ksh

CURRENT_SWITCH=""
while getopts :a:b:c: SWITCHES
do
    case $SWITCHES in
        a)    CURRENT_SWITCH="a"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        b)    CURRENT_SWITCH="b"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        c)    CURRENT_SWITCH="c"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        :)    print -n "CURRENT_SWITCH = $CURRENT_SWITCH : error : "
            print "missing argument"
            exit 1
            ;;
            
        \?)    print "unknown switch" 
            exit 1
            ;;
    esac
done

Thanks in advance,
Smilie
# 2  
Old 03-13-2009
Remove the first colon:

Code:
while getopts a:b:c: SWITCHES

# 3  
Old 03-13-2009
You used CURRENT_SWITCH in your output, which was still set to the last valid option. If there's an invalid option it's name will be set in OPTARG.
Code:
#!/bin/ksh

CURRENT_SWITCH=""
while getopts :a:b:c: SWITCHES
do
    case $SWITCHES in
        a)    CURRENT_SWITCH="a"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        b)    CURRENT_SWITCH="b"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        c)    CURRENT_SWITCH="c"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        :)    print -n "CURRENT_SWITCH = $OPTARG : error : "
            print "missing argument"
            exit 1
            ;;
            
        \?)    print "unknown switch" 
            exit 1
            ;;
    esac
done

# 4  
Old 03-13-2009
Quote:
Originally Posted by pludi
You used CURRENT_SWITCH in your output, which was still set to the last valid option. If there's an invalid option it's name will be set in OPTARG.
Code:
#!/bin/ksh

CURRENT_SWITCH=""
while getopts :a:b:c: SWITCHES
do
    case $SWITCHES in
        a)    CURRENT_SWITCH="a"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        b)    CURRENT_SWITCH="b"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        c)    CURRENT_SWITCH="c"
            print -n "The switch $CURRENT_SWITCH was selected with "
            print "the argument $OPTARG"
            ;;
            
        :)    print -n "CURRENT_SWITCH = $OPTARG : error : "
            print "missing argument"
            exit 1
            ;;
            
        \?)    print "unknown switch" 
            exit 1
            ;;
    esac
done


Thanks a lot, yes in fact my mistake was that I had to look for the last token in OPTARG and not in CURRENT_SWITCH. Now it works pretty well!

Thanks again for your help.

Kind Regards,
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Hardware

Dell XPS M1330 doesn't detect optical drive

I have a Dell XPS M1330 laptop. The OS is Windows 10. I had to replace the motherboard because it was bad. After replacing the motherboard I noticed the optical (DVD/CD) drive is neither detected in BIOS and OS nor making any noise at all. Everything else seems to be working fine. The optical drive... (3 Replies)
Discussion started by: milhan
3 Replies

2. UNIX for Beginners Questions & Answers

Getopts not echoing correctly

Hi, When I run the the following code: #!/bin/bash if ]; then usage fi if ] then echo "Do not execute this as root, use -s instead" fi SERVERFILE="servers" function usage { echo "USAGE: ${0} COMMAND" (4 Replies)
Discussion started by: mohca2020
4 Replies

3. Ubuntu

Ubuntu 16.04 doesn't detect my second monitor

I have Ubuntu 16.04 (dual boot with Windows 10) and a HP Spectre x360 laptop. I have recently bought a Dell Ultrasharp U2515H monitor, which I connect via a Dell docking station. When I plug the docking station to my laptop on Windows, both monitors are detected and everything works fine. ... (23 Replies)
Discussion started by: twelth_hour
23 Replies

4. Shell Programming and Scripting

[KornShell]: Why getopts doesn't detect disabled switches in this script?

Hi, My environement OS: Linux Fedora Core 17 X86_64 KSH version: sh (AT&T Research) 93u+ 2012-08-01 As I understand inside a Kornshell script, the getopts statement allows to collect information about the switches provided for the script (if any). Besides, it is possible to... (3 Replies)
Discussion started by: dariyoosh
3 Replies

5. AIX

AIX doesn't detect TS3200 tape library

Hi all, I have a problem here root@host1 / # lsdev -Cc tape rmt0 Defined 07-00-02 IBM 3580 Ultrium Tape Drive (FCP) rmt1 Defined 07-00-02 IBM 3580 Ultrium Tape Drive (FCP) smc0 Defined 07-00-02 IBM 3573 Tape Medium Changer (FCP) root@host1 / # cfgmgr -l fcs2 Method error... (2 Replies)
Discussion started by: h@foorsa.biz
2 Replies

6. AIX

AIX doesn't detect smc1 media changer in ts3200 tape library

Hi all, I'm having a problem here my environment as follows. AIX 5.3 TL 10 running on a P6 p520 sever with 3 fibre channel cards and Tivoli Storage Manager TSM 5.4 and TS3200 Tape Library with 2 drives and 2 media changers. My problem as follows smc1 doesn't show up , I ran cfgmgr multiple... (6 Replies)
Discussion started by: h@foorsa.biz
6 Replies

7. Shell Programming and Scripting

echo switches

Hello All, I am writing an shell script but abruptly its not able to recognize switches in echo statement. #!/bin/bash top -n 1 -b>ankit host=`hostname` time=`cat ankit|grep load|tr -s " "|cut -d " " -f3` load=`cat ankit|grep load|tr -s " "|cut -d "," -f4|cut -d ":" -f2` ... (3 Replies)
Discussion started by: ajaincv
3 Replies

8. Shell Programming and Scripting

Collect information from switches

Hi All , In my environment we have 12 SAN switches .Culd u pls help me for below queries . would like to telnet to all switches and collect information in daily basis with "switchstatus" command and store the output under /tmp/ folder on systemA . To keep passwords of 12switches in a... (1 Reply)
Discussion started by: chinni-script
1 Replies

9. SuSE

vsft doesn't work correctly

I install vsftpd server on 2 SUSE 10.2 servers. The first works perfectly, but the second doesn't work how I expect. The second works only over local network and doesn't over internet. The vsftpd.conf and ../xinetd.d/vsftpd are the same in 2 servers. The only different was when I threw to log in... (1 Reply)
Discussion started by: zhivko.neychev
1 Replies

10. UNIX for Advanced & Expert Users

Tar switches!!!

Hi, If i want to write my data on several tapes, (more than one tape), what switch(s) i need to use with tar. In other word if my data needs the sapce more than one tape & i don't wanna to compress or ... my data. so is it possible to write up to the end of the tape & it asks to put another... (1 Reply)
Discussion started by: nikk
1 Replies
Login or Register to Ask a Question