The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
rsh fails with -n option shafi2all SUN Solaris 2 07-30-2009 01:24 AM
getopts with repeat of same option CarlosNC Shell Programming and Scripting 2 10-27-2008 09:35 AM
getopts: bad option(s) JoeJoseph Shell Programming and Scripting 1 08-19-2008 03:14 PM
getopts with non-option arguments? kdelok Shell Programming and Scripting 2 07-26-2008 10:44 PM
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 12:46 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
HexKnot HexKnot is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
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.

Code:
 
#!/usr/bin/bash
OPTERR=1
while getopts :P::E::I::L::b:ftdorvxhn optn
do
  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." ;;
  esac
done
Thanks,
HexKnot
  #2 (permalink)  
Old 1 Week Ago
redhead's Avatar
redhead redhead is offline
Registered User
  
 

Join Date: Feb 2002
Location: Denmark
Posts: 66
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 (permalink)  
Old 1 Week Ago
HexKnot HexKnot is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
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.

getopts

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

Thanks
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0