getopts fails to error on option w/o dash

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts fails to error on option w/o dash
# 1  
Old 11-11-2009
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 OPTERR=0 and OPTERR=1 and don't get any errors. Do I need to write a function to catch options without dashes? Seems that would be something getopts should catch.

while getopts :P::E::I::L::b:ftdorvxhn optn
  case ${optn} in
    P)  echo "PORTX=${OPTARG}" ;;
    E)  echo "EXCLUDE_FILE=${OPTARG}" ;;
    I)  echo "IP_ARRAY=${OPTARG}" ;;
    L)  echo "IP_LIST=${OPTARG}" ;;
    b)  echo "Running function: ${OPTARG}" ;;
    f)  echo "pre_flight chk_preflight" ;;
    t)  echo "flar_size_total" ;;
    d)  echo "dupe_flar_chk" ;;
    o)  echo "old_flar_chk" ;;
    r)  echo "rotate_logs" ;;
    v)  echo "Not yet implemented." ;;
    x)  echo "get expl file" ;;
    h)  echo "USAGE 0; exit 0" ;;
    n)  echo "USAGE 1;exit 0" ;;
    \?)  echo "help or unknown" ;;
    :)  echo "ERROR: Option requires an arguement." ;;

# 2  
Old 11-11-2009
All getopts does is look for options applied in the correct manor, ie with a leading dash, which matches the required patern given to getopts, what you're trying to achieve is expecting it to catch something which it isn't looking for.
# 3  
Old 11-11-2009
Being a skeptic, I had to double check but you are correct. For the detail oriented, here's the explanation from Home | Open Source Initiative.


Specifically, this paragraph.

"When the end of options is encountered, the getopts utility shall exit with a return value greater than zero; the shell variable OPTIND shall be set to the index of the first non-option-argument, where the first "--" argument is considered to be an option-argument if there are no other non-option-arguments appearing before it, or the value "$#" +1 if there are no non-option-arguments; the name variable shall be set to the question-mark character. Any of the following shall identify the end of options: the special option "--", finding an argument that does not begin with a '-', or encountering an error."

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A dash to GOTO or a dash from GOTO, that is the question...

Well, guys I saw a question about GOTO for Python. So this gave me the inspiration to attempt a GOTO function for 'dash', (bash and ksh too). Machine: MBP OSX 10.14.3, default bash terminal, calling '#!/usr/local/bin/dash'... This is purely a fun project to see if it is possible in PURE... (3 Replies)
Discussion started by: wisecracker
3 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

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 #!/bin/sh #set -x while getopts ":n:" opt; do case "$opt" in n) host=$OPTARG shift 2 ;; *)... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

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

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

6. Solaris

rsh fails with -n option

Issue is with rsh loggin. I tried logging into solaris machine from solaris machine using rsh with login prompt, it passes. $ rsh -l USERNAME IPADDRESS <Prompt for password> : USERNAME logged in ....... $ when i try same command with -n option(required for automation), it fails, $... (2 Replies)
Discussion started by: shafi2all
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