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
相关推荐
aningx5 小时前
使用自定义bash脚本预防oom卡死 v2
linux
律宏阔5 小时前
Android 9 开发板实现系统侧滑返回
android
2601_962293535 小时前
Appium+python自动化(十二)- Android UIAutomator终极定位凶器(超详解)
android·自动化测试·appium·定位·uiautomator
mmsx5 小时前
MapLibre 自定义比例尺:Web 墨卡托屏幕距离换算公式与实现
android·源码·地图·maplibre
JoyCong19986 小时前
知识科普:ToDesk鸿蒙版高级功能免费开放使用了!
运维·服务器·华为·智能手机·harmonyos·远程工作
可乐鸡翅yeah_6 小时前
HLS CORS 跨域问题完整排查实战,解决 M3U8 与 TS 分片跨域报错
android·ios·音视频·m3u8·音视频在线播放
yu_zhidun6 小时前
企业电脑数据防泄密软件选型:内网终端 DLP 落地实战指南
服务器·网络·电脑
cjp5606 小时前
011. UG二次开发,UG管道通讯服务器与wpf客户端通讯
linux·运维·服务器
上官唐云6 小时前
2024届本科 | C++ Linux服务端开发求职:从嵌入式到底层服务端的技术之路(附开源项目+简历)
linux·c++·开源·求职招聘
Zucker n7 小时前
Ubuntu24.04结合ollama本地使用Hermes
linux·ubuntu