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
相关推荐
大聪明-PLUS1 小时前
如何使用 Docker 打包一个简单的应用程序:简易指南
linux·嵌入式·arm·smarc
执念WRD2 小时前
熊海CMS v1.0代码审计实战
android·nginx·安全·web安全·网络安全·系统安全
jllllyuz2 小时前
基于ThinkPHP实现动态ZIP压缩包的生成
android
深耕AI3 小时前
【完整教程】宝塔面板FTP配置与FileZilla连接服务器
运维·服务器
serve the people3 小时前
Prompts for Chat Models in LangChain
java·linux·langchain
李昊哲小课3 小时前
Ubuntu 24.04 MariaDB 完整安装与配置文档
linux·ubuntu·mariadb
无聊的小坏坏4 小时前
从单 Reactor 线程池到 OneThreadOneLoop:高性能网络模型的演进
服务器·网络·一个线程一个事件循环
百***92024 小时前
【MySQL】MySQL库的操作
android·数据库·mysql
AI智域边界 - Alvin Cho4 小时前
Bloomberg、LSEG 与 MCP 缺口:为什么尚未发布完整的 MCP 服务器,以及多智能体系统如何解決这问题
运维·服务器
人间打气筒(Ada)4 小时前
zerotier内网穿透部署(rockylinux部署本地服务器)超详细~~~
linux·内网穿透·内网·公网·zerotier·穿透