一个搜索字符串的脚本
June 10th, 2008
No comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#!/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