2012-12-07 63 views
0

這是我的shell腳本 -無法顯示在shell腳本選項參數

while getopts ":m" opt; do 
    case "$opt" in 
    m) 
     echo "-m was triggered! $OPTARG was entered" >&2 
     ;; 
    j) 
     echo "-j was triggered! $4 was entered" >&2 
     ;; 
    k) 
     echo "-k was triggered! $6 was entered" >&2 
     ;; 
    \?) 
     echo "Invalid option: -$OPTARG" >&2 
     ;; 
    esac 
done 
shift $(($OPTIND - 1)) 

當我運行shell腳本 -

./test.sh -m hello 

我得到的輸出此way-

-m was triggered! was entered 

我哪裏錯了?請耐心等待,因爲我對shell腳本完全陌生。

回答

1

它應該m是不是:M

while getopts "m:" opt; do 
+0

我應該怎麼做才能輸出爲'./test.sh -mytest hello'? 「mytest」選項似乎不適用於現有的代碼。 – AlwaysALearner

+0

@ user1649068:check this:http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options – Guru