shell中如何判断是否包含某个字符串
(相关资料图)
在Shell中,可以使用以下方法来判断一个字符串是否包含另一个字符串:
1. 使用`grep`命令:```shellif echo "$string" | grep -q "$substring"; then echo "String contains substring"else echo "String does not contain substring"fi```
2. 使用`[[`条件判断:```shellif [[ $string == *"$substring"* ]]; then echo "String contains substring"else echo "String does not contain substring"fi```
3. 使用`case`语句:```shellcase "$string" in *"$substring"*) echo "String contains substring" ;; *) echo "String does not contain substring" ;;esac```
关键词:
相关新闻