2011-04-14 55 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