[Linux] 批量把不规则的Android应用程序命名为[中文_版本号](在无中文名称时命名为英文)

#!/bin/bash - 
#===============================================================================
#
#          FILE: apk_rename.sh
# 
#         USAGE: ./apk_rename.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: linkscue (scue), linkscue@gmail.com
#       CREATED: 2013年04月15日 15时17分18秒 HKT
#     COPYRIGHT: Copyright (c) 2013, linkscue
#      REVISION: 0.1
#  ORGANIZATION: ATX风雅组
#===============================================================================

set -o nounset                              # Treat unset variables as an error
if [[ $# != 1 ]]; then
    echo "usage: $(basename $0) <apk_dir>"
    exit 0
fi
dir="$1"

#-------------------------------------------------------------------------------
#  相关的函数
#-------------------------------------------------------------------------------
en_label ()                                     # 获取英文名称
{
    aapt d badging "$1" | awk -n -F"'" '/application-label:/ {print $2}'| sed 's/ //g' 2>/dev/null
}

cn_label ()                                     # 获取中文名称
{
    aapt d badging "$1" | awk -n -F"'" '/application-label-zh_CN/ {print $2}' | sed 's/ //g' 2>/dev/null
}
zh_label ()
{
    aapt d badging "$1" | awk -n -F"'" '/application-label-zh:/ {print $2}' | sed 's/ //g' 2>/dev/null
}
version ()
{
    aapt d badging "$1" | awk -n -F"'" '/versionName/ {print $6}'| sed 's/ //g' 2>/dev/null
}
activity ()                                     # 获取活动
{
    aapt d badging "$1" | awk -n -F"'" '/launchable-activity/ {print $2}' | sed 's/ //g' 2>/dev/null
}

#-------------------------------------------------------------------------------
#  变量替换方式把' '替换为'_'
#-------------------------------------------------------------------------------
find $dir -name "*.apk" -type f | while read line;            # 只显示文件
    do tmp=\"$line\";                           # 获取文件
    echo $tmp;                                  # 为xargs准备
    echo ${tmp// /_} ;                          # 变量替换 
done | xargs -n 2 mv -v 2> /dev/null

find $dir -name "*.apk" -type f | while read apk
do
    version_name=$(version "$apk")
    cn_label_name=$(cn_label "$apk")
    if [[ "$cn_label_name" != "" ]]; then
        new_name="${cn_label_name// /_}"_"$version_name".apk
        echo "mv: $(basename "$apk")   --> $new_name"
        mv "$apk" "$dir/$new_name"
    else
        zh_label_name=$(zh_label "$apk")
        if [[ "$zh_label_name" != "" ]]; then
            new_name="${zh_label_name// /_}"_"$version_name".apk
            echo "mv: $(basename "$apk")   --> $new_name"
            mv "$apk" "$dir/$new_name"
        else
            en_label_name=$(en_label "$apk")
            if [[ "$en_label_name" != "" ]]; then
                new_name="${en_label_name// /_}"_"$version_name".apk
                echo "mv: $(basename "$apk")   --> $new_name"
                mv "$apk" "$dir/$new_name"
            fi
        fi

    fi
done 2> /dev/null

 

posted @ 2013-04-15 15:46  scue  阅读(342)  评论(0编辑  收藏  举报