Sponsored Content
Top Forums Shell Programming and Scripting Why getopts doesn't detect correctly my switches? Post 302297440 by dariyoosh on Friday 13th of March 2009 11:35:56 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Getopt::Std(3pm)					 Perl Programmers Reference Guide					  Getopt::Std(3pm)

NAME
getopt, getopts - Process single-character switches with switch clustering SYNOPSIS
use Getopt::Std; getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side effect. getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts getopts('oif:'); # -o & -i are boolean flags, -f takes an argument # Sets $opt_* as a side effect. getopts('oif:', \%opts); # options as above. Values in %opts DESCRIPTION
The getopt() function processes single-character switches with switch clustering. Pass one argument which is a string containing all switches that take an argument. For each switch found, sets $opt_x (where x is the switch name) to the value of the argument if an argument is expected, or 1 otherwise. Switches which take an argument don't care whether there is a space between the switch and the argument. The getopts() function is similar, but you should pass to it the list of all switches to be recognized. If unspecified switches are found on the command-line, the user will be warned that an unknown option was given. The getopts() function returns true unless an invalid option was found. Note that, if your code is running under the recommended "use strict 'vars'" pragma, you will need to declare these package variables with "our": our($opt_x, $opt_y); For those of you who don't like additional global variables being created, getopt() and getopts() will also accept a hash reference as an optional second argument. Hash keys will be x (where x is the switch name) with key values the value of the argument or 1 if no argument is specified. To allow programs to process arguments that look like switches, but aren't, both functions will stop processing switches when they see the argument "--". The "--" will be removed from @ARGV. "--help" and "--version" If "-" is not a recognized switch letter, getopts() supports arguments "--help" and "--version". If "main::HELP_MESSAGE()" and/or "main::VERSION_MESSAGE()" are defined, they are called; the arguments are the output file handle, the name of option-processing package, its version, and the switches string. If the subroutines are not defined, an attempt is made to generate intelligent messages; for best results, define $main::VERSION. If embedded documentation (in pod format, see perlpod) is detected in the script, "--help" will also show how to access the documentation. Note that due to excessive paranoia, if $Getopt::Std::STANDARD_HELP_VERSION isn't true (the default is false), then the messages are printed on STDERR, and the processing continues after the messages are printed. This being the opposite of the standard-conforming behaviour, it is strongly recommended to set $Getopt::Std::STANDARD_HELP_VERSION to true. One can change the output file handle of the messages by setting $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of "--help" (without the "Usage:" line) and "--version" by calling functions help_mess() and version_mess() with the switches string as an argument. perl v5.18.2 2014-01-06 Getopt::Std(3pm)
All times are GMT -4. The time now is 05:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy