shell替换文件 shell替换文件中的文件路径脚本分享

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

shell替换文件 shell替换文件中的文件路径脚本分享

  2021-03-19 我要评论
想了解shell替换文件中的文件路径脚本讲解的相关内容吗,在本文为您仔细讲解shell替换文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们