字符编码转换工具 shell实现字符编码转换工具分享

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

字符编码转换工具 shell实现字符编码转换工具分享

  2021-03-19 我要评论
想了解shell实现字符编码转换工具讲解的相关内容吗,在本文为您仔细讲解字符编码转换工具的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:shell,字符编码转换,下面大家一起来学习吧。

复制代码 代码如下:

#!/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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们