shell脚本 -- 用途替换所有json文件(main.json 除外)中的 ios下载链接

shell脚本 -- 用途替换所有json文件(main.json 除外)中的 ios下载链接,第1张

#!/bin/bash
#脚本用途 替换所有json文件(main.json 除外)中的 ios下载链接
#调用格式 "xxx.sh" (脚本名称) "ios||and" (参数1)  "https:xxxx" (参数2)
#调用示例 *sh and https:andxxxxx.apk
#调用示例 *sh ios https:andxxxxx.ios
#执行时 会备份源文件到当前目录bak_json下

cd "$(dirname $0)"
pwd
json_remove="main.json" #替换排除文件
#json_files=$(find . -maxdepth 1 -name "*.json" ! -name ${json_remove})
#find 查找会存在文件名为./影响文件备份

if [ "" == "ios" -o "" == "and" ] && [ "" != "" ]; then

    for js_file in $(ls | grep .*json | grep -v ${json_remove}); do

        if [ -f ${js_file} ]; then

            if [ "" == "ios" ]; then

                old_url=$(grep -oP ios.*?http.*?\" ${js_file} | awk -F[\"] '{print }')
            else
                old_url=$(grep -oP and.*?http.*apk.*?\" ${js_file} | awk -F[\"] '{print }')
            fi

                now_url=

            if [ "$old_url" != "" ]; then
                #开始备份
                mkdir -p bak_json && cp ${js_file} bak_json/$(date +"%F")-${js_file}

                #开始替换
                sed -i "s#$old_url#$now_url#g" ${js_file}
                echo "替换文件 ${js_file}$old_url$now_url"
            else
                echo "没有匹配到旧的url 文件: ${js_file}"
            fi

        fi

    done

else

    echo "格式  \"xxx.sh\" (脚本名称) \"ios||and\" (参数1)  \"https:xxxx\" (参数2)"
fi

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存