• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > shell实现字符编码转换工具分享

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了shell 截取字符,shell 判断字符,shell 过滤字符,shell if 字符,shell转义字符等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

: << 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
</div>

</div>

您可能想查找下面的文章:

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

相关文章

  • shell脚本编程之循环语句学习笔记
  • Shell根据日期、月份、星期判断年份的代码
  • Shell中创建序列和数组(list、array)的方法
  • Shell常见用法小记
  • Shell脚本中通过正则表达式匹配IP地址
  • 使用shc工具加密shell脚本详解
  • Shell脚本定义变量和重新赋值
  • shell中冒号的特殊用法分享
  • Shell使用Epoch进行日期时间转换和计算的几个小函数
  • 获取服务器信息的Shell脚本分享(ubuntu、centos测试通过)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • linux shell实现随机数几种方法分享(date,random,uuid)
    • Centos下查看网卡的实时流量命令
    • CentOS下对shell脚本加密的二种方法
    • Shell脚本实现监控swap空间使用情况和查看占用swap的进程
    • shell编程基础知识小结
    • shell脚本编程之循环语句学习笔记
    • Shell使用Epoch进行日期时间转换和计算的几个小函数
    • Linux shell脚本全面学习入门
    • linux数据库备份并通过ftp上传脚本分享
    • 收集的48个Shell脚本小技巧

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有