1. 判断字符串包含的基本语法 在Shell脚本中,可以使用条件表达式和通配符来判断字符串是否包含另一个字符串。 2. 使用if语句结合字符串包含判断 可以使用[[ ]]条件表达式和=~操作符(针对bash等支持扩展的Shell)来进行正则表达式匹配,或者使用case语句和通配符*来进行模式匹配。 3. 示例代码 示例一:使用正则表达式(...
if [ int1 -eq int2 ] 如果int1等于int2 if [ int1 -ne int2 ] 如果不等于 if [ int1 -ge int2 ] 如果>= if [ int1 -gt int2 ] 如果> if [ int1 -le int2 ] 如果<= if [ int1 -lt int2 ]如果< 3、文件的判断 [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。 [ ...
1、通过grep来判断: str1="abcdefgh" str2="def" result=$(echo $str1 | grep "${str2}") if [[ "$result" != "" ]] then echo "包含" else echo "不包含" fi 先打印长字符串,然后在长字符串中 grep 查找要搜索的字符串,用变量result记录结果,如果结果不为空,说明str1包含str2。如果结果...
cat file |grep "hello" || echo "hello is not contain" 五、if语句举例(一)判断传入脚本的参数个数 直接上代码: #!/bin/bash if (( $# < 3 ));then echo "The number of parameters is less than 3" exit -1 fi echo "parameters ok:$@" 把上面代码保存为test1.sh并给予可执行权限,直接运...
使用 if-else 比较字符串 #!/bin/bash string1=Debian string2=RHEL if [ "$string1" = "$...
Linux shell if 判断字符串包含子串 string='My long string' if [[ $string == *"My long"* ]] || [[ $string == *"my long"* ]]; then echo "It's there" fi
shell 判断字符串是否包含另一个字符串 grep s1="abcdefg" s2="bcd" result=$(echo $s1 | grep "${s2}") if [[ "$result" != "" ]] then echo "$s1 include $s2" else echo "$1 not include $s2" fi 1. 2. 3. 4. 5. 6.
linux shell if判断字符串是否包含某字符串,判断${line}中是否包含:dmccsif[["${line}"=~":dmccs"]];thentmp=${line#git@gitee.com:dmccs/};fi