linux批量轉換整個目錄下的文件編碼為UTF-8

jopen 9年前發布 | 4K 次閱讀 Shell Android Studio

    #!/bin/bash -

#===============================================================================  
#  
#          FILE: conv.sh  
#   
#         USAGE: ./conv.sh   
#   
#   DESCRIPTION:   
#   
#       OPTIONS: ---  
#  REQUIREMENTS: ---  
#          BUGS: 目前不支持傳入參數中含有空格;  
#         NOTES: ---  
#        AUTHOR: linkscue (scue), linkscue@gmail.com  
#       CREATED: 2013年03月06日 22時52分31秒 HKT  
#     COPYRIGHT: Copyright (c) 2013, linkscue  
#      REVISION: 0.1  
#  ORGANIZATION: ---  
#===============================================================================  

__ScriptVersion="0.1"  

#===  FUNCTION  ================================================================  
#         NAME:  usage  
#  DESCRIPTION:  Display usage information.  
#===============================================================================  
function usage ()  
{  
        cat <<- EOT  

  Usage :  $0 -s suffix1 -s suffix2 -d dir1 -d dir2 -f file1 -f file2  

  Options:  
  -h|help       Display this message  
  -v|version    Display script version  
  -s suffix     Setting suffix  
  -d directory  Convert all file encoding to UTF-8  
  -f file       Convert a file encoding to UTF-8  

EOT  
}    # ----------  end of function usage  ----------  

#-----------------------------------------------------------------------  
#  Handle command line arguments  
#-----------------------------------------------------------------------  

suffixs=()  
directorys=()  
files=()  
while getopts ":hvd:f:s:" opt  
do  
  case $opt in  

    h|help     )  usage; exit 0   ;;  

    v|version  )  echo "$0 -- Version $__ScriptVersion"; exit 0   ;;  

    f  )  files+=("$OPTARG")   ;;  

    d  )  directorys+=("$OPTARG")   ;;  

    s  )  suffixs+=("$OPTARG")   ;;  

    \? )  echo -e "\n  Option does not exist : $OPTARG\n"  
          usage; exit 1   ;;  

  esac    # --- end of case ---  
done  
shift $(($OPTIND-1))  

# 檢查輸入  
if [[ ${#files} -lt 1 ]] && [[ ${#directorys} -lt 1 ]]; then  
    usage  
    exit  
fi  

TMPFILE="$(mktemp -t convXXXXXX)"  
trap "rm -f '$TMPFILE'" 0               # EXIT  
trap "rm -f '$TMPFILE'; exit 1" 2       # INT  
trap "rm -f '$TMPFILE'; exit 1" 1 15    # HUP TERM  

#-------------------------------------------------------------------------------  
#  轉換編碼函數  
#-------------------------------------------------------------------------------  
conv_utf8(){  
    file="$1"  
    echo "處理文件: '$file' ..."  
    iconv -f gb2312 -t UTF-8 "$file" -o $TMPFILE 2> /dev/null &&\  
        mv -f $TMPFILE "$f" || {  
        echo "轉換失敗: '${file}'"  
    }  
}  

# 轉換文件  
for f in "${files[@]}"; do  
    conv_utf8 "$f"  
done  

# 轉換目錄文件  
if [[ ${#directorys} -gt 1 ]]; then  
    if [[ ${#suffixs} -lt 1 ]]; then  
        echo  
        echo "請指定需轉換編碼的文件后綴,如 '-s txt -s java'"  
        echo  
        usage  
        exit  
    else  
        for s in "${suffixs[@]}"; do  
            for f in $(find $directorys -type f -name "*.${s#.}"); do  
                conv_utf8 "$f"  
            done  
        done  
    fi  
fi  </pre> 


假如把這個腳本內容保存至~/bin/conv,并給予權限;

使用舉例:

1. conv -f file1 -f file2 -s java -s xml -d dir1 -d dir2

2. conv -s java -d .

3. conv -s java -s xml -d android_helloworld

4. conv -f file1 -f file2

有了這個轉換編碼,把Windows下的工程引入到Linux上開發就輕松得多了,不再有什么字符編碼讀不出來的問題;

來自:http://blog.csdn.net/qq_20480611/article/details/48135463

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!