如何匹配两个字符串之一作为 bash 中的条件
How to match one of two strings as a condition in bash
首先我有这样一个非常简单的脚本
#!/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
它有效,但我想添加另一个正确的值,该值也将 return“支持您的系统”,让我传递脚本以进一步发展
例如,如果文件 /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
但它给我一个错误,我什至不确定这是否是正确的语法。
谁能帮帮我?
也许使用 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
试试这个:
#!/bin/sh
grep -qE 'AlmaLinux|RockyLinux 8' /etc/redhat-release
if [ $? ]; then
echo "your system is supported"
else
echo "Unsupported OS"
fi
其他完全有效的检测:
#!/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
首先我有这样一个非常简单的脚本
#!/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
它有效,但我想添加另一个正确的值,该值也将 return“支持您的系统”,让我传递脚本以进一步发展
例如,如果文件 /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
但它给我一个错误,我什至不确定这是否是正确的语法。
谁能帮帮我?
也许使用 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
试试这个:
#!/bin/sh
grep -qE 'AlmaLinux|RockyLinux 8' /etc/redhat-release
if [ $? ]; then
echo "your system is supported"
else
echo "Unsupported OS"
fi
其他完全有效的检测:
#!/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