Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getopts does not recognize question mark

I am currently parsing options in a script like that:

while getopts ":ia" OptionArgument; do
case $OptionArgument in
    i ) echo "bli";;
    a ) echo "bla";;
    * ) echo "flag not known";;
    ? ) echo "unknown parameter";;
esac
done

Every parsing option and flag works - except this one: ? ). So if I call my script with something like ./MyScript hjrfgdskjgh it passes the loop correctly - although it should be catched in the last line with ? ).
I also tried removing quotes from ":ia"or using \? or . ) - nothing works! What do I do wrong??

like image 936
Malvin Avatar asked Dec 09 '25 23:12

Malvin


2 Answers

? character has a special meaning, see http://tldp.org/LDP/abs/html/special-chars.html#WILDCARDQU You can't use it unescaped or unquoted. You have to call the script with ./script.sh -'?' or ./script.sh -\?

like image 118
giordano Avatar answered Dec 13 '25 03:12

giordano


* matches anything. Putting anything below it is meaningless, it would only be matched if the * does not match it.

like image 42
choroba Avatar answered Dec 13 '25 04:12

choroba