文件批量转码Shell脚本实现(这里以gbk18030转utf8为例)

来源:互联网 发布:小米网络解锁 编辑:程序博客网 时间:2024/06/01 07:37

(亲测可用)

#!/bin/bash#print the derectory and file #待转码的文件所在的目录for file in /data/yulong/qa/*doif [ -d "$file" ]then  echo "$file is directory"elif [ -f "$file" ]then  file1=`basename $file`  #转码后文件存储的目录  dir=/data/yulong/qa_utf8/  file2=$dir${file1%%.*}_utf8.csv  iconv -f gb18030 -t utf8  $file > $file2  echo "$file --gb18030--->--utf8-------> $file2"fidone

测试结果如下(显示转码成功):
这里写图片描述

更完善点如下(加了转码错误判断,没有测试,可能有问题):

#!/bin/bash#print the derectory and file #此处目录为待转码的目录for file in /data/yulong/qa/*doif [ -d "$file" ]then  echo "$file is directory" elif [ -f "$file" ]then  file1=`basename $file`  #转码后文件存储在目录  dir=/data/yulong/qa_utf8/  file2=$dir${file1%%.*}_utf8.csv  echo "$file ----> $file2"  iconv -f gb18030 -t utf8  $file > $file2  if [ $? -eq 1 ]     then         echo "$file 转码失败"        continue  fifidone

参考文章:Linuxshell字符串截取与拼接

原创粉丝点击