直接错误的标志输入

时间:2016-01-11 08:33:29

标签: bash shell if-statement

我有一个bash脚本,它带有一堆标志。我希望它将空的或错误的输入(与任何标志不匹配的输入)指向某个show_help函数。我已经将空输入部分关闭,并且在运行带有错误标志的脚本时,我得到错误输入的错误。但是 - 当使用适当的标志运行时,我仍然会收到错误输入的错误。

我认为我的elif语句出了问题。我是否需要在值之间使用逗号?还有别的吗?

剧本:

f_read_input "$@"

# direct wrong or empty flag input to show help
if [ -z "$@" ]; then
  echo "Please enter at least one flag (you entered none)."
  echo
  f_show_help
elif [ "$@" != $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

1 个答案:

答案 0 :(得分:1)

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
}