LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2019, 04:07 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,020

Rep: Reputation: 75
OPTARG in getopt shows the option instead of the option arguments


I'm not sure why this is happening exactly, but when I use a random option for getopt (other than h and t), OPTARG actually shows the option itself, instead of what I'd expect from OPTARG. Why is that happening actually with the question mark? With the t) option there a clear difference between the two, so it is as expected.


Code:
#!/bin/bash

while getopts ":t:h" OPT; do
     case ${OPT} in
       h) echo "Usage $0 [-h] --> displays this useless help menu"
          exit 1
       ;;
       t) echo "This is a valid but also a useless option: ${OPT} ${OPTARG}"
          exit 1
       ;;
       \?) echo "You have provided an invalid option \"${OPTARG}\". Bitte versuch es nochmal." 1>&2
          exit 1
       ;;
    esac
done
 
Old 08-07-2019, 06:02 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,742

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
This is the expected behaviour and documented, at least for
Bash's builtin getopts:
Code:
$ help getopts
...
    getopts reports errors in one of two ways.  If the first character
    of OPTSTRING is a colon, getopts uses silent error reporting.  In
    this mode, no error messages are printed.  If an invalid option is
    seen, getopts places the option character found into OPTARG.  If a
    required argument is not found, getopts places a ':' into NAME and
    sets OPTARG to the option character found.  If getopts is not in
    silent mode, and an invalid option is seen, getopts places '?' into
    NAME and unsets OPTARG.  If a required argument is not found, a '?'
    is placed in NAME, OPTARG is unset, and a diagnostic message is
    printed.
...
 
1 members found this post helpful.
Old 08-07-2019, 06:19 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,784

Rep: Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805
"?" is a getopts reserved character. In a nutshell, if getopts finds an unknown option or a misplaced argument it will set the value of $OPT to a question mark "?" or if the option is valid but an expected argument is not found, $OPT is set to "?".

A bit late...

Last edited by michaelk; 08-07-2019 at 06:21 PM.
 
Old 08-13-2019, 05:53 PM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,020

Original Poster
Rep: Reputation: 75
Great! Thank you both for your answer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to set flags for non valid arguments getopt? BW-userx Programming 18 08-07-2017 12:44 PM
BASH script using getopt to parse optional arguments nano2 General 6 04-28-2011 08:09 AM
shell script: can getopt process more than one argument per option? BrianK Programming 2 12-13-2007 05:11 PM
Getopt() newbie... AMMullan Programming 6 02-11-2004 01:02 PM
Using getopt in cplus UltimaGuy Programming 3 08-28-2003 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration