Android res复制脚本

./copy.sh A路径 B路径 文件名 key

./copy.sh A_res B_res strings.xml hello

将A_res文件为strings.xml中的hello,复制到项目B_res文件为strings.xml中

复制代码
#!/bin/bash

# 检查输入的参数数量
if [ "$#" -ne 4 ]; then
    echo "用法: $0 <源路径> <目标路径> <目标文件名> <关键字>"
    exit 1
fi

# 获取输入的四个参数

oldPath=$1         # 源路径
newPath=$2         # 目标路径
targetFile=$3      # 目标文件名
keyword=$4         # 要查找的关键字

# 定义获取字符串中心内容的函数
function getCenter {
    local one=$1
    local theLeft=`echo ${one#*\">}`
    local theCenter=`echo ${theLeft%<\/*}`
    echo "$theCenter"
}

# 定义删除文件末尾多余空行的函数
function deleteBlankRow() {
    local line=$1
    local a=`awk '{if($0 != "") {i = 0} else {i++}} END {print i}' $line` # 获取末尾空行数
    if [ "$a" -ne 0 ]; then
        for ((i=1; i<=a; i++)); do
            sed -i -e '$ {/^$/d;}' $line # 删除末尾空行
        done
    fi
}

# 进入源路径并查找指定文件
cd "$oldPath" || exit
find . -name "$targetFile" -type f | cut -c3- > stringList.txt

# 定义注释内容
strt="<!--SR: modify by $(whoami) $(date +%Y%m%d) start-->"
endd="<!--SR: modify by $(whoami) $(date +%Y%m%d) end-->"

# 循环读取文件列表中的每一行
while read -r line; do
    ss=`grep -wh "$keyword" "$line"`  # 在文件中查找关键字
    if [ -z "$ss" ]; then
        continue
    fi

    # 切换到目标路径
    cd "$newPath" || exit
    if [ -e "$line" ]; then
        ww=`grep -wh "$keyword" "$line"`
        if [ -z "$ww" ]; then
            # 如果目标文件中没有关键字内容
            deleteBlankRow "$line"
            sed -i '$d' "$line"
            echo -e "$ss" >> "$line"
            echo "</resources>" >> "$line"
            #dos2unix "$line"  # 转换文件格式,去掉 ^M
            cd "$oldPath" || exit
            continue
        else
            # 如果目标文件中有关键字内容,比较字符串
            L=`grep -wn "$keyword" "$line" | cut -d : -f 1`
            nr=`sed -n "${L}p" "$line"`
            nrCenter=`getCenter "$nr"`
            ssCenter=`getCenter "$ss"`
            if [[ "$ssCenter" == "$nrCenter" ]]; then
                cd "$oldPath" || exit
                echo "相等"
                continue
            else
                echo "不相等"
            fi

            # 移植字符串并添加注释
            theLeft=`echo ${nr%\">*}`
            kn=`echo "${nr%%<*}" | wc -L`
            sed -i "${L}d" "$line"
            ssRight=`echo ${ss#*\">}`
            ssNew=$theLeft\">$ssRight
            for (( j=1; j<$kn; j++ )); do
                strt=" ${strt}"
                endd=" ${endd}"
                ssNew=" ${ssNew}"
            done
            sed -i "${L}i\\ ${ssNew}" "$line"
            sed -i "${L}i\\ ${strt}" "$line"
            sed -i "$((L+2))i\\ ${endd}" "$line"
            #dos2unix "$line"  # 转换文件格式,去掉 ^M
            cd "$oldPath" || exit
            continue
        fi
    else
        echo "$line 文件不存在"
        cd "$oldPath" || exit
        continue
    fi
done < stringList.txt

# 清理临时文件
rm stringList.txt
相关推荐
optimistic_chen1 分钟前
【Docker入门】容器技术
linux·运维·服务器·docker·容器
Lueeee.3 分钟前
2.智梯云枢・全维管控广告系统——解决串口卡顿 + 优化稳定性
linux·运维·服务器
南行*3 分钟前
C语言Linux环境编程
linux·c语言·开发语言·网络安全
海清河晏1114 分钟前
Linux进阶篇:HTTP协议
linux·运维·http
June`6 分钟前
IO模型全解析:从阻塞到异步(高并发的reactor模型)
linux·服务器·网络·c++
ASS-ASH12 分钟前
快速处理虚拟机磁盘扩容问题
linux·数据库·vmware·虚拟机·磁盘扩容
AI_567812 分钟前
零基础学Linux:21天从“命令小白”到独立部署服务器
linux·服务器·人工智能·github
不染尘.15 分钟前
Linux基本概述
linux·windows·centos·ssh
Winston Wood15 分钟前
Android图形与显示系统经典故障解决方案:从源码到实操
android·图形系统·显示系统
weixin_4462608518 分钟前
XPipe: 轻松访问你的服务器基础设施 [特殊字符]
运维·服务器