shell脚本将gbk文件转化为utf-8

前端之家收集整理的这篇文章主要介绍了shell脚本将gbk文件转化为utf-8前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用注意项:

原来文件格式gbk的,否则可能出现utf-8转utf-8乱码。

  1. #!/bin/bash
  2.  
  3. function gbk2utf(){
  4. file="$1"
  5. echo "处理文件 '$file' ..."
  6. iconv -f gb2312 -t UTF-8 -c "$file" > tmp
  7. mv -f tmp "$file"
  8. }
  9.  
  10. function getdir(){
  11. for element in `ls $1`
  12. do
  13. dir_or_file=$1"/"$element
  14. echo $dir_or_file
  15. if [ -d $dir_or_file ]
  16. then
  17. getdir $dir_or_file
  18. else
  19. gbk2utf $dir_or_file
  20. fi
  21. done
  22. }
  23. getdir $1
  1.  
  1. 运行:sh gbk2utf.sh dir

猜你在找的Bash相关文章