gpt4 book ai didi

bash - 如何在 bash case 命令中执行 "else"?

转载 作者:行者123 更新时间:2023-11-29 09:23:38 25 4
gpt4 key购买 nike

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 来做。

最佳答案

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

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

关于bash - 如何在 bash case 命令中执行 "else"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com