使用 if 语句在变量中查找字符串

hhh*_*505 11 string variables bash

我目前正在尝试在输出如下内容的变量中找到一个字符串:

一二三

我的代码:

echo "please enter one,two or three)
read var

var1=one,two,threee

if [[ "$var" == $var1 ]]; then
    echo "$var is in the list"
else
    echo "$var is not in the list"
fi
Run Code Online (Sandbox Code Playgroud)

编辑2:

我试过这个,但仍然不匹配。你是正确的,因为它匹配部分匹配,所以它不匹配先前答案中的确切字符串。

 groups="$(aws iam list-groups --output text | awk '{print tolower($5)}' | sed '$!s/$/,/' | tr -d '\n')"
echo "please enter data"
read "var"

if [ ",${var}," = *",${groups},"* ]; then
    echo "$var is in the list"
else
    echo "$var is not in the list"
fi
Run Code Online (Sandbox Code Playgroud)

尝试这个它仍然不匹配我需要的确切字符串。

Die*_*ano 3

可能还有其他问题(例如匹配部分单词),但如果您使用=~(match) 而不是它,则==它适用于简单的情况

if [[ "$var1" =~ "$var" ]]; then
   ...
Run Code Online (Sandbox Code Playgroud)