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

shell脚本中使用iconv实现批量文件转码的代码分享

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

junjie 通过本文主要向大家介绍了shell脚本代码,shell脚本读取文件,shell脚本创建文件,shell脚本删除文件,shell脚本写文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在开发中,我们经常需要对N多文件编码进行更改,iconv只能对单文件的进行更改,怎么办呢?我们写一个shell脚本来解决这个问题。

例子一:使用shell脚本实现批量转码的操作。

#!/bin/sh 
#
convertCodeFilePath=$1 
fromCode=$2 
toCode=$3 
 
for i in {1..1} 
do 
  [ -f $convertCodeFilePath ] 
  if [ $? -eq 0 ] 
  then 
    iconv -f $fromCode -t $toCode -c -o $convertCodeFilePath $convertCodeFilePath 
    if [ $? -ne 0 ] 
    then 
      echo $convertCodeFilePath "=>" convert code failed.      
    else 
      echo $convertCodeFilePath "=>" convert code success. 
    fi 
    break; 
  fi 
   
  [ -d $convertCodeFilePath ] 
  if [ $? -ne 0 ] 
  then 
    break; 
  fi 
     
  dir=`ls $convertCodeFilePath | sort -d` 
 
  for fileName in $dir 
  do 
    fileFullPatch=$convertCodeFilePath/$fileName 
     
    fileType=`echo $fileName |awk -F. '{print $2}'` 
     
    [ -d $fileName ] 
    if [ $? -eq 0 ] 
    then 
      continue 
    fi 
     
    if [ $fileType != 'sh' ] && [ $fileType != 'py' ] && [ $fileType != 'xml' ] && [ $fileType != 'properties' ] \ 
    && [ $fileType != 'q' ] && [ $fileType != 'hql' ] && [ $fileType != 'txt' ] 
    then 
      continue 
    fi 
     
    iconv -f $fromCode -t $toCode -c -o $fileFullPatch $fileFullPatch 
    if [ $? -ne 0 ] 
    then 
      echo $fileName "=>" convert code failed. 
      continue 
    else 
      echo $fileName "=>" convert code success. 
    fi     
  done 
done
</div>

使用方式:

sh convertCode.sh /home/sam/data gbk utf-8
</div>

例子二:简洁版

用法示例:

cd ~/workspace/XXXProject
 
~/iconv_shell.sh ./ *java
</div>

好了,直接上代码~~

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

这里还有些修改的余地,比如源和目标编码应该作为参数,我暂时没有这个需求,懒得写了。

</div>

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

  • shell脚本nicenumber实现代码
  • Shell脚本实现的基于SVN的代码提交量统计工具
  • Shell脚本实现C语言代码行数统计
  • Shell脚本实现批量下载网络图片代码分享
  • Shell脚本实现的阳历转农历代码分享
  • Shell脚本实现批量下载网络图片代码分享
  • Shell脚本实现的阳历转农历代码分享
  • shell脚本中使用iconv实现批量文件转码的代码分享
  • 自动重启服务的shell脚本代码
  • shell脚本运行5秒后自动退出的代码

相关文章

  • 利用linux的timerfd_create实现计时器示例分享
  • Shell脚本中单引号(‘)和双引号(“)的使用区别
  • Shell脚本实现C语言代码行数统计
  • getcwd cannot access parent directories错误解决方法
  • linux批量备份服务器配置文件和目录的脚本
  • shell脚本自动修复mysql损坏的表
  • shell两个文件去重的多种姿势
  • 详解Linux 操作系统下安装rpm包的方法步骤
  • Shell多线程操作及线程数控制实例
  • shell的比较运算介绍

文章分类

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

最近更新的内容

    • php在linux中可能用到的命令(推荐)
    • shell自定义函数的6个特点总结
    • linux基础之Shell Script入门介绍
    • Shell脚本定期清空大于1G的日志文件
    • linux启动级别的含义(init 0-6)
    • linux下安装oracle后使用命令行启动的方法 linux启动oracle
    • linux bash中too many arguments问题的解决方法
    • 使用shell脚本分析网站日志统计PV、404、500等数据
    • 数值运算shell脚本
    • shell 批量压缩指定目录及子目录内图片的方法

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

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