Shell scripts convert files from GBK to UTF-8 encoded _linux shell

Source: Internet
Author: User

Shell script:

#!/bin/sh # # # Convert file from GB2312 to UTF-8 # path= "$" unset opt if ["$" = "Force"]; Then opt= "-c-s" fi if [-Z "$path"]; Then echo "Nusage: $ <file or dir>n" elif [!-e "$path"]; Then echo "Nerror:destination: $path does not exist.n" fi if [F "$path"]; Then echo "Converting $path (GBK--> utf-8) ... "If File" $path "|grep-q UTF-8 >/dev/null; Then echo "already converted" Else iconv-f GBK $opt-T Utf-8 "$path" >/tmp/$$.tmp if [$?-eq 0]; Then echo "Success" Mv-f/tmp/$$.tmp "$path" Else echo "Failed" fi fi elif [-D "$path"]; Then Path= ' echo ' $path/' |sed ' s//////' find ' $path '-path ' $path. * "-prune-o-type-f-print|while read I do dir= ' di Rname $i ' file= ' basename $i ' echo ' converting $dir/$file (GBK--> utf-8) ... "Iconv-f gbk-t utf-8 $opt" $i " ; /tmp/$$.tmp 2>/dev/null if [$-eq 0]; Then echo "Success" Mv-f/tmp/$$.tmp "$i" Else echo "Failed" fi done fi

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.