2011-04-14 49 views
1

说我有以下脚本来处理选项:[?]庆典1.4.3无效选项

while getopts dsf opts 2>/dev/null 
do 
    case $opts in 
    d) echo "d";; 
    s) echo "s";; 
    \?) echo "Error: An invalid option [?] was entered."; 
     exit 1;; 
    esac 
done 

我想更换与无效开关我进入。 所以,如果我进入

./myscript -z //output: Error: An invalid option [-z] was entered. 

我怎么会赶上无效开关?使用我的$ opts变量显示一个问号。

回答

5

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. 
+0

人,信息,帮助总是问一个问题之前开始的好地方:) – mathk 2011-04-14 18:41:52