#!/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
心情: 一般