http://www.meichua.com (收藏,设为首页)
以前打牌的时候出牌太慢,其他人就叫着出啊,出啊,所以就变成了chua...... (手机请访问 http://3g.dlog.cn/meichua)

一个搜索字符串的脚本

2008年6月10日(Tuesday) 17点23分 作者: chua 天气: 心情: 一般

#!/bin/sh
if [ $# -ne 3 ] ; then
  cat << EOF >&2
  commandLine: $0 string file-or-directory needExit
  sample1 检查文件目录: findString.sh abc /home/user true
  sample2 检查文件列表: findString.sh abc file.txt true
EOF
  exit 1
fi

echo ""
echo "+------------------------------------+"
echo "|    start templates's checking      |"
echo "+------------------------------------+"

#定义检查结果未false
error=false

#检查目录中的文件
if [ -d $2 ] ; then
  #查找vm后缀文件,每个文件查找字符串,xargs把每个结果传入grep
  output=`find $2 -name "*.vm" |xargs grep -n -o $1`
  if [ ! -z "$output" ]; then
     
      #输出是一个"result1 result2 result3"的字符串,把字符串转成数组,逐个输出
      #for i in `echo $output`
      #do
      #   echo $i
      #done
     
      #输出是一个"result1 result2 result3"的字符串,用awk split分割,再逐个输出
      echo "$output" | awk '{split($1,myarray," ")}{for(i in myarray) {print myarray[i]}}'
      error=true
  fi
#检查文件类标中的文件
else
    while read LINE
    do

        #查找字符串,输入文件名,错误行,匹配字符串
        output=`grep -n -o $1 $LINE`
        if [ ! -z "$output" ]; then
                echo $LINE":"$output
                error=true
        fi
    done < $2
fi

echo "+------------------------------------+"
echo "|    templates's checking is over    |"
echo "+------------------------------------+"
echo ""
if $error; then
    echo "*******************************CHECKING RESULT WARNING***********************************"
    if $3; then
        echo ""
        echo -e "Template contained the String \"$1\", continue the  proceess? [y/n]:\c"
        read process
        if [ $process == "N" -o $process == "n" ]; then
            exit 1;
        else
            exit 0;
        fi
    fi
fi

执行
bin/findString.sh member.getMemberId fileList.txt true
bin/findString.sh member.getMemberId deploy/templates true

标签: xargs awk grep 
姓名: 
邮箱:  {可选}
网址:  {可选} 此评论只有我和写日记的人查阅
校验码: ... <我看不清楚>
网记为您提供手机和互联网同步的个人主页,带给你不一样的体验