用于判断字符串之间是否存在包含关系的shell/ target=_blank class=infotextkey>shell脚本,有需要的朋友拿去。
#!/bin/bash
var1="hello"
var2="he"
#方法1
if [ ${var1:0:2} = $var2 ]
then
echo "1:include"
fi
#方法2
echo "$var1" |grep -q "$var2"
if [ $? -eq 0 ]
then
echo "2:include"
fi
#方法3
echo "$var1" |grep -q "$var2" && echo "include" ||echo "not"
#方法4
[[ "${var1/$var2/}" != "$var2" ]] && echo "include" || echo "not"
其他方法参考:
expr或linuxjishu/13830.html target=_blank class=infotextkey>awk的index函数
${var#...}
${var%...}
${var/.../...}