当前位置: > shell编程 >

linux shell脚本替换文件中文件路径

时间:2015-01-14 12:54来源:linux.it.net.cn 作者:IT
一例shell脚本,用于替换文件中的文件路径,可以指定需要替换路径记录的文本、替换的文件目录、替换的文件类型。

此shell脚本的用法:
参数1、需要替换路径记录的文本,格式:老路径 老名称 新路径 新名称
参数2、需要替换的文件的目录
参数3、需要替换的文件的类型

代码:
 

#!/bin/sh

function _replace(){
basepath=$0
> $basepath.log
i=1
while read line
do
        echo "start==============" >> $basepath.log
        oldpath=`echo $line|awk '{print $1$2}'|sed 's/^\\///g'`
        newpath=`echo $line|awk '{print $3$4}'|sed 's/^\\///g'`
        echo $oldpath >> $basepath.log
        echo $newpath >> $basepath.log
        if [ "$oldpath" != "" ] && [ "$newpath" != "" ];then
                refiles=`grep $oldpath -irl --include="*.$3" $2`
                for refile in $refiles
                do
                        sed -i 's#'$oldpath'#'$newpath'#ig' $refile
                        echo $refile >> $basepath.log
                done
        fi
        echo "end===========================" >> $basepath.log
        echo "=">> $basepath.log
        echo $i >> $basepath.log
        echo "=">> $basepath.log
i=$(($i+1))
done < $1
}

if [ "$1" != "" ] || [ "$2" != "" ] || [ "$3" != "" ];then
_replace $1 $2 $3
else
echo "use like this:$basepath file filepath type"
fi

 

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容