用Shell Script导出SVN版本间变更的文件
#!/bin/bash
# 配置
# TODO 将配置信息保存在外部文件中以方便产生不同目标的导出。
START=9981
END=10054
VER=eom
BUILD=v1
OUTPUT_PATH=builds/$VER/$BUILD/files/
# 清理
if [ -d "$OUTPUT_PATH" ]; then
rm -rf $OUTPUT_PATH
fi
# 得到更改过的文件数量和文件名列表
# svn diff --summarize 返回"Status \t Filename"
COUNT=`svn diff --summarize -r $START:$END | wc -l`
Diff=`svn diff --summarize -r $START:$END`
until [ $COUNT -lt "1" ]
do
# echo "$Diff" 的用法也许不合适。
# awk pattern action, 'print $2' 即输出第二列的内容
# grep -v 反向选择,即不含模式的。此为“选取路径不以builds开头的文件”
File="`echo "$Diff" | awk {'print $2'} | grep "^builds" -v | head -$COUNT | tail -1`"
Dir=`dirname $File`
Name=`basename $File`
# 检查目标路径,如不存在则递归的创建
if [ ! -d "$OUTPUT_PATH$Dir" ]; then
mkdir $OUTPUT_PATH$Dir -p
fi
# 将文件中的string1替换为string2,并且输出到目标路径
# 此处是将两个操作合并,也可先做cp,再替换
sed 's/string1/string2/g' $File > $OUTPUT_PATH$Dir/$Name
# 如果目标文件中含有string3,则将之替换为string4,在目标文件上操作。
if grep "string3" $File > /dev/null
then
sed -in-place -e 's/string3/string4/g' $OUTOUT_PATH$Dir/$Name
fi
# 去除string5
if grep "string5" $File > /dev/null
then
sed -in-place -e 's/string5\///g' $OUTPUT_PATH$Dir/$Name
fi
# more replace here
# TODO
# replace configure variables, need define configure file
# TODO
COUNT=`expr $COUNT - 1`
done
# export changed database table
# TODO
# commit to svn
# TODO
另外记录一条批量替换字串的命令:
sed -i 's/pattern/new/g' `grep -rl 'pattern' *` # sed -i: in place,在原文件上操作 # grep -r: recursive,递归的,遍历所有子目录下的文件 # grep -l: file with matches,返回包含有模式的文件