alwayshelloworld 2012-02-26
第一个版本:
用法示例:
cd ~/workspace/XXXProject ~/iconv_shell.sh ./ *java
#!/bin/bash if [ "$#" != "2" ]; then echo "Usage: `basename $0` dir filter" exit fi dir=$1 filter=$2 echo $1 for file in `find $dir -name "$2"`; do echo "$file" iconv -f gbk -t utf8 -o $file $file done
另一个版本:
用法和功能:
1.有两个参数,即需要转换的文件和希望转换的编码。
2.使用 iconv 工具转换,并将目标编码名称加入到文件名中以示区别。#!/bin/bash #Author:digglife #Description:Convert the charset of text file. #version:0.2 #Date:2011/05/12 #To Be Resovled: # 1.get filename without using $fileExtension varable. #Get the parameters file=$1 to_charset=$2 #Get the first character of the $file first_char=${file:0:1} #Test if the file exists,break if it doesn't if [ ! -f "$file" ];then echo "$file,No such file or directory" exit 1 #If the given $file contains the directory,get the basename of it,and change dir to its directory. elif [ first_char="/" ];then full_filename=$(basename $file) cd $(dirname $file) else full_filename=$file fi #Split the file extention and filename file_extension=$(echo $full_filename | sed 's/^.*.//g') filename=${full_filename%.$file_extension} #Get the original charset charset_org=$(file -I $full_filename | sed 's/^.*charset=//g') #Convert the charset,and change the full filename iconv -c -f $charset_org -t $to_charset $full_filename > $filename.$to_charset.$file_extension
Shell编码知识总结: