![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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
HexKnot |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|