如何在(负)Bash 条件中使用 OR

发布于 2024-12-03 17:17:51 字数 309 浏览 1 评论 0原文

我的脚本的这一部分检查发行版是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独岁月 2024-12-10 17:17:51

你可以使用类似的东西:

rel="$(lsb_release -is)"
if [[ "${rel}" != "Ubuntu" && "${rel}" != "Arch" ]]; then
   # Neither Ubuntu nor Arch
fi

You can use something like:

rel="$(lsb_release -is)"
if [[ "${rel}" != "Ubuntu" && "${rel}" != "Arch" ]]; then
   # Neither Ubuntu nor Arch
fi
锦欢 2024-12-10 17:17:51

使用 case/esac 结构

case $(lsb_release -is) in
  Ubuntu|Arch ) echo "Ubuntu or Arch found";;
  * ) 
 echo "Neither Ubuntu nor Arch!"
 read -p "Continue anyway(y/N)? "
 sleep 0
 [ "$REPLY" == "y" ] || exit
 ;;
esac

Use a case/esac construct

case $(lsb_release -is) in
  Ubuntu|Arch ) echo "Ubuntu or Arch found";;
  * ) 
 echo "Neither Ubuntu nor Arch!"
 read -p "Continue anyway(y/N)? "
 sleep 0
 [ "$REPLY" == "y" ] || exit
 ;;
esac
眼角的笑意。 2024-12-10 17:17:51

最接近原始代码的是:

if [[ ! $(lsb_release -is) =~ Ubuntu|Arch ]]; then 
    echo "Neither Ubuntu nor Arch!"
    read -p "Continue anyway(y/N)? "
    sleep 0
    [ "$REPLY" == "y" ] || exit; 
fi

这使用 bash 3 中引入的匹配运算符。另请注意,上述内容在 bash 3.2 中有效,在此之前您需要对模式使用引号。

如果你没有 bash 3 你可以使用 grep

if ! lsb_release -is| egrep -q 'Ubuntu|Arch'; then 
    echo "Neither Ubuntu nor Arch!"
    read -p "Continue anyway(y/N)? "
    sleep 0
    [ "$REPLY" == "y" ] || exit; 
fi

注意 -q 是 grep 的非标准选项

Closest to your original code would be:

if [[ ! $(lsb_release -is) =~ Ubuntu|Arch ]]; then 
    echo "Neither Ubuntu nor Arch!"
    read -p "Continue anyway(y/N)? "
    sleep 0
    [ "$REPLY" == "y" ] || exit; 
fi

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

if ! lsb_release -is| egrep -q 'Ubuntu|Arch'; then 
    echo "Neither Ubuntu nor Arch!"
    read -p "Continue anyway(y/N)? "
    sleep 0
    [ "$REPLY" == "y" ] || exit; 
fi

Note that -q is a non-standard option of grep

凉风有信 2024-12-10 17:17:51

为了回答我自己的问题,这里有一个稍长但更灵活的方法来实现相同的结果。

rel="$(lsb_release -is)"
if [[ "${rel}" = "Arch" ]]; then
    echo "It's Arch"
  elif [[ "${rel}" = "Ubuntu" ]]; then
    echo "It's Ubuntu"
  else
    echo "It's Neither"
    read -p "Continue anyway(y/N)? "
    sleep 0
    [ "$REPLY" == "y" ] || exit
fi

To answer my own question, here is a slightly longer but more flexible way of achieving the same result.

rel="$(lsb_release -is)"
if [[ "${rel}" = "Arch" ]]; then
    echo "It's Arch"
  elif [[ "${rel}" = "Ubuntu" ]]; then
    echo "It's Ubuntu"
  else
    echo "It's Neither"
    read -p "Continue anyway(y/N)? "
    sleep 0
    [ "$REPLY" == "y" ] || exit
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文