网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

shell中怎么实现一个字符编码转换工具-创新互联

shell中怎么实现一个字符编码转换工具,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

专注于为中小企业提供网站制作、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业盐津免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

#!/bin/bash

: << mark
转码工具,支持UTF-8转GBK和GBK转UTF-8
孔令飞@2012-05-07
mark

#set -x

scode="gbk"
dcode="utf-8"

function Usage()
{
 cat << EOF
Usage: conv [OPTIONS] [DIR]
[-u] GBK to UTF-8
[-g] UTF-8 to GBK
EOF
 exit 1
}


#将当前目录下所有普通文件进行转码 GBK to UTF-8
function g2u()
{
 local dir=$1
 printf "Convering $dir ......"
 for file in $(ls $dir)
 do
  file="$dir/$file"
  if [ -f $file ];then

   coding=$(file -b $file | cut -d ' ' -f1)

   #进行转码
   if [ "$coding" = "ISO-8859" ];then
    local tmpfile=$(mktemp)
    Fright=$(stat -c %a $file)
    Fuser=$(stat -c %U $file)
    Fgro=$(stat -c %G $file)
    iconv -f $scode -t $dcode $file > $tmpfile || Usage
    mv $tmpfile $file &&
    chmod $Fright $file
    chown $Fuser:$Fgrp $file
   fi
  fi
 done
 printf "      done\n"
}

function u2g()
{
 local dir=$1
 printf "Convering $dir ......"
 for file in $(ls $dir)
 do
  file="$dir/$file"
  if [ -f $file ];then

   coding=$(file -b $file |cut -d ' ' -f1)

   #进行转码
   if [ "$coding" = "UTF-8" ];then
    local tmpfile=$(mktemp)
    Fright=$(stat -c %a $file)
    Fuser=$(stat -c %U $file)
    Fgro=$(stat -c %G $file)
    iconv -f $dcode -t $scode $file > $tmpfile || Usage
    mv $tmpfile $file &&
    chmod $Fright $file
    chown $Fuser:$Fgrp $file
   fi
  fi
 done
 printf "      done\n"
}

[ $# -ne 2 ] && Usage

while getopts ug opt
do
 case $opt in
  u) echo "Convert gbk coding to utf-8 ...."
  for dir in $(find $2 -type d)
  do
   g2u $dir
  done

  g) echo "Convert utf-8 coding to gbk ...."
  for dir in $(find $2 -type d)
  do
   u2g $dir
  done

  *) Usage
  exit 1

 esac
done

exit 0


看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


本文名称:shell中怎么实现一个字符编码转换工具-创新互联
URL网址:http://bjjierui.cn/article/dipdde.html

其他资讯