linux批量修改文件字符集shell脚本


linux批量修改文件字符集shell脚本
 
需求:需要将 “/home/zichen/dxzs0305”目录下所有PHP文件字符集由 UTF-8
转换为GB2312  www.2cto.com  
shell脚本内容:
[html] 
#!/bin/bash -x  
SUBFIX="php"  #需要转换的目标文件后缀    www.2cto.com  
cd /home/zichen/dxzs0305     #目标文件根目录  
  
if [ -z $1 ];then  
    cd $PWD  
else  
    if [ -d $1 ];then  
        cd $1  
    else  
        echo " $1 is not exist;"  
        exit 1  
    fi  
fi  
  
for i in $SUBFIX;  
do  
    files=`find . -name "*.$i"`  
    for f in $files;  
    do  
        type=`file $f|awk -F':' '{print $2}' |awk  '{print $1}'` #获取文件类型  
        if [ $type != "ISO-8859" ];then  
            iconv -f UTF-8 -t GB2312 -o $f $f   #使用  iconv函数进行转换   
          #  enca -L UTF-8 -x zh_cn $f   #如果使用enca 进行转换,需要安装 enca    命令 sudo apt-get  install  enca   
        else  
            echo "$f encoding is :$type"  
        fi  
  
    done  
done  
 iconv 函数参数简介:
iconv命令用于转换指定文件的编码,默认输出到标准输出设备,亦可指定输出文件。
用法: iconv [选项...] [文件...]
有如下选项可用:  www.2cto.com  
输入/输出格式规范:
-f, --from-code=名称 原始文本编码
-t, --to-code=名称 输出编码
信息:
-l, --list 列举所有已知的字符集
输出控制:
-c 从输出中忽略无效的字符
-o, --output=FILE 输出文件
-s, --silent 关闭警告
--verbose 打印进度信息
-?, --help 给出该系统求助列表
--usage 给出简要的用法信息
-V, --version 打印程序版本号
 

相关内容

    暂无相关文章