在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
替换为你想要匹配的字符串。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: shell发送post请求报错怎么解决