如何在(负)Bash 条件中使用 OR
我的脚本的这一部分检查发行版是 Ubuntu 还是 Arch。问题是我不知道用什么来替换 OR 才能使其工作。我尝试了 -o
和来自各个网站的其他建议,但没有成功。
if [ ! $(lsb_release -is) == "Ubuntu" OR "Arch" ]; then
echo "Neither Ubuntu nor Arch!"
read -p "Continue anyway(y/N)? "
sleep 0
[ "$REPLY" == "y" ] || exit
fi
This section of my script checks whether the distro is either Ubuntu or Arch. The problem is that I cannot figure out what to replace the OR with to make it work. I tried -o
and other suggestions from various websites without succes.
if [ ! $(lsb_release -is) == "Ubuntu" OR "Arch" ]; then
echo "Neither Ubuntu nor Arch!"
read -p "Continue anyway(y/N)? "
sleep 0
[ "$REPLY" == "y" ] || exit
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以使用类似的东西:
You can use something like:
使用
case/esac
结构Use a
case/esac
construct最接近原始代码的是:
这使用 bash 3 中引入的匹配运算符。另请注意,上述内容在 bash 3.2 中有效,在此之前您需要对模式使用引号。
如果你没有 bash 3 你可以使用 grep
注意 -q 是 grep 的非标准选项
Closest to your original code would be:
This uses the match operator introduced in bash 3. Also note that the above is valid in bash 3.2, prior to that you need to use quotes for the pattern.
if you don't have bash 3 you can use grep
Note that -q is a non-standard option of grep
为了回答我自己的问题,这里有一个稍长但更灵活的方法来实现相同的结果。
To answer my own question, here is a slightly longer but more flexible way of achieving the same result.