随笔-314  评论-209  文章-0  trackbacks-0

#! /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或awk的index函数
${var#...}                 
${var%...}
${var/.../...}

posted on 2011-03-04 15:16 xzc 阅读(26417) 评论(1)  编辑  收藏 所属分类: linux/unix

评论:
# re: shell 判断字符串是否存在包含关系 2011-03-04 15:22 | xzc
#是否包含@INFILE@字符串的判断
if echo "$ctl_file"|grep -q "@INFILE@"
then
#分隔符前字符串
echo "${ctl_file%%@INFILE@*}" >${table_name}.ctl
#文件名
echo "${infile}" >>${table_name}.ctl
#分隔符后字符串
echo "${ctl_file##*@INFILE@}" >>${table_name}.ctl
else
echo "${ctl_file}" >${table_name}.ctl
fi  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: