2016-01-11 52 views
2

我有一個bash腳本,它帶有一堆標誌。我希望它指向空的或錯誤的輸入(不匹配任何標誌的輸入)到一些show_help函數。我已經清空了輸入部分,並且在運行具有錯誤標誌的腳本時,我得到錯誤輸入的錯誤。但是 - 當使用正確的標誌運行時,我仍然得到錯誤輸入的錯誤。直接輸入錯誤的標誌

我認爲我的elif聲明有問題。我是否需要在值之間使用逗號?還有別的嗎?

腳本:

f_read_input "[email protected]" 

# direct wrong or empty flag input to show help 
if [ -z "[email protected]" ]; then 
    echo "Please enter at least one flag (you entered none)." 
    echo 
    f_show_help 
elif [ "[email protected]" != $FLAG_HELP,$FLAG_TARLogs,$FLAG_TARResrc,$FLAG_DELLogs,$FLAG_TAREncod,$FLAG_DELResrc,$FLAG_DELEncod ]; then 
    echo "One or more of the flags entered are wrong. Please check your spelling and see the list of flags below." 
    echo 
# f_show_help 
fi 
+0

使用getopts的或getopt的或case語句。 – 123

回答

0
while getopts ahs: option 
do 
    case "$option" 
    in 
     $FLAG_HELP) code/function;; 
     $FLAG_TARLogs) code/function;; 
     $FLAG_TARResrc) code/function ;; 
     $FLAG_DELLogs) code/function;; 
     $FLAG_TAREncod) code/function;; 
     $FLAG_DELResrc) code/function;; 
     $FLAG_DELEncod) code/function;; 
     h) DisplayUsage 
     exit 0;; 
     ?) DisplayUsage 
     exit 1;; 
    esac 
done 2>/dev/null 

顯示功能包含的選項,以及如何使用它們..

DisplayUsage() 
{ 
echo 
echo 
echo "Options are " 
echo 
echo 
echo 
echo 
} 
+0

是否沒有辦法將@ $與value1或2或3(比如@ $!= value1'或'2'或'3')進行比較? –

+1

將選項保存在列表中,然後嘗試使用此 '[[$ list =〜$ @]] && echo'yes'|| echo'no'' yes表示正確選項no表示不正確 – Grv