[Linux] 批量转换整个目录下的文件编码为UTF-8;
1 #!/bin/bash - 2 #=============================================================================== 3 # 4 # FILE: conv.sh 5 # 6 # USAGE: ./conv.sh 7 # 8 # DESCRIPTION: 9 # 10 # OPTIONS: --- 11 # REQUIREMENTS: --- 12 # BUGS: 目前不支持传入参数中含有空格; 13 # NOTES: --- 14 # AUTHOR: linkscue (scue), linkscue@gmail.com 15 # CREATED: 2013年03月06日 22时52分31秒 HKT 16 # COPYRIGHT: Copyright (c) 2013, linkscue 17 # REVISION: 0.1 18 # ORGANIZATION: --- 19 #=============================================================================== 20 21 __ScriptVersion="0.1" 22 23 #=== FUNCTION ================================================================ 24 # NAME: usage 25 # DESCRIPTION: Display usage information. 26 #=============================================================================== 27 function usage () 28 { 29 cat <<- EOT 30 31 Usage : $0 -s suffix1 -s suffix2 -d dir1 -d dir2 -f file1 -f file2 32 33 Options: 34 -h|help Display this message 35 -v|version Display script version 36 -s suffix Setting suffix 37 -d directory Convert all file encoding to UTF-8 38 -f file Convert a file encoding to UTF-8 39 40 EOT 41 } # ---------- end of function usage ---------- 42 43 #----------------------------------------------------------------------- 44 # Handle command line arguments 45 #----------------------------------------------------------------------- 46 47 suffixs=() 48 directorys=() 49 files=() 50 while getopts ":hvd:f:s:" opt 51 do 52 case $opt in 53 54 h|help ) usage; exit 0 ;; 55 56 v|version ) echo "$0 -- Version $__ScriptVersion"; exit 0 ;; 57 58 f ) files+=("$OPTARG") ;; 59 60 d ) directorys+=("$OPTARG") ;; 61 62 s ) suffixs+=("$OPTARG") ;; 63 64 \? ) echo -e "\n Option does not exist : $OPTARG\n" 65 usage; exit 1 ;; 66 67 esac # --- end of case --- 68 done 69 shift $(($OPTIND-1)) 70 71 # 检查输入 72 if [[ ${#files} -lt 1 ]] && [[ ${#directorys} -lt 1 ]]; then 73 usage 74 exit 75 fi 76 77 TMPFILE="$(mktemp -t convXXXXXX)" 78 trap "rm -f '$TMPFILE'" 0 # EXIT 79 trap "rm -f '$TMPFILE'; exit 1" 2 # INT 80 trap "rm -f '$TMPFILE'; exit 1" 1 15 # HUP TERM 81 82 #------------------------------------------------------------------------------- 83 # 转换编码函数 84 #------------------------------------------------------------------------------- 85 conv_utf8(){ 86 file="$1" 87 echo "处理文件: '$file' ..." 88 iconv -f gb2312 -t UTF-8 "$file" -o $TMPFILE 2> /dev/null &&\ 89 mv -f $TMPFILE "$f" || { 90 echo "转换失败: '${file}'" 91 } 92 } 93 94 # 转换文件 95 for f in "${files[@]}"; do 96 conv_utf8 "$f" 97 done 98 99 # 转换目录文件 100 if [[ ${#directorys} -gt 1 ]]; then 101 if [[ ${#suffixs} -lt 1 ]]; then 102 echo 103 echo "请指定需转换编码的文件后缀,如 '-s txt -s java'" 104 echo 105 usage 106 exit 107 else 108 for s in "${suffixs[@]}"; do 109 for f in $(find $directorys -type f -name "*.${s#.}"); do 110 conv_utf8 "$f" 111 done 112 done 113 fi 114 fi
假如把这个脚本内容保存至~/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上开发就轻松得多了,不再有什么字符编码读不出来的问题;