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
相关推荐
太子釢21 小时前
用 AI 完整实现 Github 客户端:基于 KMP + SwiftUI + Compose
android·人工智能·ios
大鹏的NLP博客21 小时前
通用 Linux 嵌入式板端 C/C++ ABI 与 Glibc 依赖治理规范
linux·c++·交叉编译
汪海游龙21 小时前
本地源码还是 Maven 版本?Gradle composite build 双轨依赖的正确接法
android·ci/cd·kotlin
k3x1n1 天前
三星安卓系统BUG排查记录:云闪付、铁路12306、个人所得税、中国移动等App,长期闪退的根本原因分析
android·逆向
tedcloud1231 天前
book-to-skill 怎么部署?把技术书和项目文档变成 AI Agent 可复用知识
linux·运维·服务器·人工智能·开源
旗开得胜马到成功1 天前
SK海力士SSD售后翻车:企业备份介质裸奔风险与中科热备技术选型拆解
运维·服务器·网络
瀚高PG实验室1 天前
SQL优化案例:使用分区表优化SQL查询性能
linux·服务器·数据库·sql·microsoft·postgresql
青禾8371 天前
Linux 系统信息、权限管理与 Python 并发编程(协程与线程)完全指南
linux·python
2301_800954991 天前
Linux 常用系统信息与进程管理命令速查
linux·运维·服务器
网安蟹佬霸1 天前
CTF Web方向解题全攻略:从信息收集到getshell(附实战payload与脚本)
android·前端·网络·安全·web安全·网络安全·网安