在Shell脚本中,可以使用以下方法来判断变量是否包含某个字符串:
if
语句和[[ ... ]]
条件判断结构:if [[ $variable == *substring* ]]; then
echo "Variable contains substring"
else
echo "Variable does not contain substring"
fi
if
语句和case
结构:case $variable in
*substring*)
echo "Variable contains substring"
;;
*)
echo "Variable does not contain substring"
;;
esac
if
语句和字符串匹配操作符=
:if [ "${variable}" = *substring* ]; then
echo "Variable contains substring"
else
echo "Variable does not contain substring"
fi
请根据你的具体需求选择其中一种方法来判断变量是否包含某个字符串,将variable
替换为你想要判断的变量名,substring
替换为你想要匹配的字符串。