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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [KornShell]: Why getopts doesn't detect disabled switches in this script?
# 1  
Old 03-07-2014
Question [Solved] [KornShell]: Why getopts doesn't detect disabled switches in this script?

Hi,

My environement
Code:
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 enable/disable those switches respectively by preceding each switch letter with either - or +.

I just wanted to see this in pratice. Therefore I wrote the following code which unfortunately does not give the expected output.
Code:
#!/usr/bin/ksh
while getopts :xy arguments
do
    case $arguments in
        x)
            print "x on"
            ;;
        +x)
            print "x off"
            ;;
        y)
            print "y on"
            ;;
        +y)
            print "y off"
            ;;
        \?)
            print "unknown switch"
            ;;
    esac
done

And here is what I get when I run the above script in my linux terminal (myscript.ksh is the name of my script)
Code:
$ ./myscript.ksh -x
x on
$ ./myscript.ksh +x
$ ./myscript.ksh -y
y on
$ ./myscript.ksh +y
$

As you can see whenever a switch is preceded by - (therefore enabled) the script prints the corresponding message but whenever it is preceded by the + (disabled) the scripts doesn't seem to do anything and there is no message printed.

Could you kindly help me understand the problem in my script?

Thanks in advance,
# 2  
Old 03-07-2014
Could you point out where in the manual page it shows that getopts treats arguments as switches (+x / -x) and not options (-x)?
# 3  
Old 03-07-2014
With 1993 and laters versions of ksh, getopts will handle both + and - if you tell it you want it to do that. Change:
Code:
while getopts :xy arguments

to:
Code:
while getopts +:xy arguments

or:
Code:
while getopts :+xy arguments

The November 16, 1988 version of ksh behaved as if the first character of the option string was always '+'.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-08-2014
Quote:
Originally Posted by Don Cragun
With 1993 and laters versions of ksh, getopts will handle both + and - if you tell it you want it to do that. Change:
Code:
while getopts :xy arguments

to:
Code:
while getopts +:xy arguments

or:
Code:
while getopts :+xy arguments

The November 16, 1988 version of ksh behaved as if the first character of the option string was always '+'.
Dear Don, thank you very much for your help which solved the problem! Smilie

In fact I saw the use of getopts for switches in the book "KornShell Programming Tutorial" written by Barry Rosenberg for the first time. The author had used :xy and not :+xy. Therefore, either the example in the book had been written for an older version of KSH or simply a typo.

Anyway, thanks to your remark I learnt a new thing today & now the example works.

Thank you very much for your help. Smilie

Regards,
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. 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

3. 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

4. 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

5. Shell Programming and Scripting

write a perl script or kornshell reading a two files and outputting to comma format

Hello Can someone help me to write a perl script or kornshell reading a two files and outputting to comma format. Here is the two files listofdisks.txt id, diskname, diskgroup, diskisze(GB), FC 1, CN34, GRP1, 30, FC_CN34 2, CN67, GRP5, 19, 4, VD1, GRP4, 23, FC_VD1 6, CF_D1, ... (0 Replies)
Discussion started by: deiow
0 Replies

6. Shell Programming and Scripting

Help with Kornshell Script

Hi, I'm a novice at programming and need some help with a kornshell script I've been writting. I have an inputdirectory with all my .shp files. In my input directory the shapefiles are named XXXX_original.shp, XXXX_UPDATE.shp ect. In my .ksh script I have created a for loop which... (2 Replies)
Discussion started by: beery
2 Replies

7. Shell Programming and Scripting

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: $ ./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... (3 Replies)
Discussion started by: dariyoosh
3 Replies

8. Shell Programming and Scripting

How to make a script (Bash, KornShell, etc.) executable by mouse clicking?

Hello everybody, Is there any way to make a script (Bash, KornShell, etc.) executable by mouse clicking? For example you have a file myscript.sh, you run: $ chmod u+x myscript.sh Therefore it becomes executable and all you need is to run from the terminal: $./myscript.sh... (2 Replies)
Discussion started by: dariyoosh
2 Replies

9. Shell Programming and Scripting

Need Help with KornShell script

I need a KornShell script that will, among all the users currently logged on to the system, find a slot of one hour that contains the most number of users. I know how to list all the users currently logged on but how do I do anything with the times that are listed? Please help, thanks. (1 Reply)
Discussion started by: ckrieger1
1 Replies

10. UNIX for Advanced & Expert Users

mailx in kornshell script passing return code to CA-Unicenter

I have a KornShell script that has successfully been scheduled through Cron. We are in the process of changing over from Cron to using CA-Unicenter. To negative test the script I put a bad return code in. At the bottom of the script an e-mail is sent using mailx and then the return code is set... (1 Reply)
Discussion started by: Connie
1 Replies
Login or Register to Ask a Question