在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
替換為你想要匹配的字符串。