shell 如何将两个字符串中的一个匹配为bash中的条件

daupos2t  于 2022-11-16  发布在  Shell
关注(0)|答案(3)|浏览(124)

首先,我有一个非常简单脚本,

#!/bin/sh
if cat /etc/redhat-release | grep -q 'AlmaLinux'; then
    echo "your system is supported"
    MY SCRIPT HERE
else
    echo "Unsupported OS"
    exit0;
fi

它可以工作,但我想添加另一个正确的值,该值也将返回“支持您的系统”,并让我传递该脚本以进一步执行操作
因此,例如,如果文件/etc/redhat-release将包含AlmaLinux或Rockylinux 8,则它将同时适用于AlmaLinux和Rockylinux,但如果它将包含Centos 6,则它将无法进一步运行
我试着这样沿着:

#!/bin/sh
if cat '/etc/redhat-release' | grep -q 'AlmaLinux'|| | grep -q 'RockyLinux 8'; then
    echo "your system is supported"
else
    echo "Unsupported OS"
fi

但它给了我一个错误,我甚至不确定这是否是一个正确的语法。
有人能帮我吗?

ff29svar

ff29svar1#

也许通过使用regex pattern

#!/bin/sh
if cat '/etc/redhat-release' | grep -q -E 'AlmaLinux|RockyLinux 8'; then
    echo "your system is supported"
else
    echo "Unsupported OS"
fi
brqmpdu1

brqmpdu12#

其他完全有效的检测:

#!/bin/sh

NAME=unknown
VERSION='??'
if . /etc/os-release && case $NAME in
  *Rocky*)
    case $VERSION in
      8*) ;;
      *) false ;;
    esac
    ;;
  *AlmaLinux*) ;;
  *) false ;;
esac
then
  printf 'Your system %s version %s is supported\n' "$NAME" "$VERSION"
else
  printf '%s %s is not supported!\n' "$NAME" "$VERSION" >&2
  exit 1
fi
72qzrwbm

72qzrwbm3#

试试看:

#!/bin/sh
grep -qE 'AlmaLinux|RockyLinux 8' /etc/redhat-release
if [ $? ]; then
  echo "your system is supported"
else
  echo "Unsupported OS"
fi

相关问题