Linux GB2312 turn UTF-8 shell script __linux

Source: Internet
Author: User

Reprint please indicate source http://blog.csdn.net/imred/article/details/40951125

Under Linux to see some txt files often appear some garbled problem, so I wrote a shell script to GB2312 code into UTF-8 code

#!/bin/bash
#格式:./gb2312_2_utf8.sh path name

ifsbackup= $IFS							#备份IFS变量
ifs=$ (echo-en "\n\b")						# Set the IFS variable without spaces to prevent exception

dst=$ (Echo $1|sed ' s/\/$//') #如果路径末尾有 "/" with spaces in the file name,					Delete, and then add for
file in $ (ls "$DST" | grep. txt) do
  gb2312file= $dst \/$file
  utf8file=$ (echo "$GB 2312file" |sed ' s/.txt$/-utf8.txt/')
  string=$ (file "$GB 2312file" |grep Unicode)			#简单判断文件是否为Unicode文件, if yes, do not convert if
  ["$string" = "]
  Then
    iconv-f gb18030-t utf-8 "$GB 2312file" > "$utf 8file"	#GB18030编码, it is a superset of GB2312
  else
    echo "$ Gb2312file ' is Unicode text file
  fi

ifs= $IFSBACKUP

exit 0

Reprint please indicate source http://blog.csdn.net/imred/article/details/40951125


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.