目标是验证字符串是否与任何列表项匹配。
相反代码应类似于:
email="[email protected]"
list=["app-1", "app-2", "john"]
if [ $email -contains $list]; then
echo "email contains variable from list"
else
echo "email does not contain any of list items"
fi
# Item1: false -> email does not contains "app-1"
# Item2: false -> email does not contains "app-2"
# Item3: true -> email contains "john"
尝试:
#!/bin/bash
email="[email protected]"
list=("app-1" "app-2" "john")
found=false
for item in "${list[@]}"; do
if [[ $email == *"$item"* ]]; then
echo "Found!"
found=true
break
fi
done
if [ $found == false ]; then
echo "Not found."
fi