UNIX艺术

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

为了便于在当前目录下查找某些文件,替换其中的某些字符串,编写了一个简单脚本。其实第一个参数可以不需要,默认为当前路径即可。

脚本名:rs,意为replace string

View Code
 1 # replace strings in multiple files
 2 if [ $# -ne 4 ]
 3 then
 4         echo "usage: $0 work_folder file_filter str1 str2"
 5         echo "       If you want to use blank instead of str1, set str2 as \"__null__\""
 6         exit 1
 7 fi
 8 
 9 workdir=$1
10 filename=$2
11 from_str=$3
12 to_str=$4
13 
14 #echo "to_str=$to_str"
15 if [ $to_str = "__null__" ]
16 then
17         to_str=""
18 fi
19 #echo "to_str=$to_str"
20 
21 if [ ! -d $workdir ]
22 then
23         echo "[Error]: work folder [$workdir] does not exist"
24         exit 2
25 fi
26 
27 num=`find $workdir -name "*$filename*" -print|wc -l`
28 
29 if [ $num -le 0 ]
30 then
31         echo "[Error]: there is no [$filename] in [$workdir]"
32         exit 3
33 fi
34 
35 find $workdir -name "*$filename*" -print| while read ff
36 do
37         echo "Processing $ff..."
38     if [ -f $ff ]
39     then
40         sed -i "s/$from_str/$to_str/g" $ff
41     else
42         echo "[Info]: Ignore non-regular file $ff"
43     fi
44 done
45 
46 echo "Done!"

 

posted on 2013-02-10 19:08  jinhuawang76  阅读(181)  评论(0编辑  收藏  举报