如何在bash case命令中执行“else”?

时间:2015-02-16 16:40:07

标签: bash

filter="yes"
phonenumbers=(123 456 789 987 654 321)
echo "checking inbox now..."
myphonenumber=(7892)

if [[ "$filter" == "yes" ]]; then
    case "${phonenumbers[@]}" in
        *"$myphonenumber"*) echo "filter is on, phone number matches" 
        ;;
        !="$myphonenumber") echo "filter is on but the phone number doesn't match" 
        ;;
    esac
fi

if [[ "$filter" == "no" ]]; then
    echo "filter off"
fi

我尝试运行该脚本但它不起作用,我该如何显示filter is on but the phone number doesn't match部分?我还在学习,我知道我可以用if else语句来做,但我想知道我是否可以用case来做。

1 个答案:

答案 0 :(得分:3)

解决了这个*)所以整个代码是:

filter="yes"
phonenumbers=(123 456 789 987 654 321)
echo "checking inbox now..."
myphonenumber=(7892)

if [[ "$filter" == "yes" ]]; then
  case "${phonenumbers[@]}" in
    *"$myphonenumber"*) echo "filter is on, phone number matches" ;;
    *)                  echo "filter is on but the phone number doesn't match" ;;
  esac
fi

if [[ "$filter" == "no" ]]; then
  echo "filter off"
fi