Created
May 20, 2016 05:21
-
-
Save AndrewTsao/7da076d454f8066f0c885f79a460f1e0 to your computer and use it in GitHub Desktop.
使用BASH对文本文件进行编码格式转换格
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
has_bom() | |
{ | |
head -c3 "$1" | grep -qP '\xef\xbb\xbf'; | |
} | |
utf8bom() | |
{ | |
echo "convert " $1 | |
has_bom $1 | |
if [ $? -ne 1 ]; then | |
echo "ignore utf8 file " $1 | |
return 0 | |
fi | |
echo -ne '\xEF\xBB\xBF' > $1.tmp | |
iconv -f GB2312 -t UTF-8 $1 >> $1.tmp | |
if [ $? -ne 0 ]; then | |
echo "error" $1 | |
rm $1.tmp | |
return 1 | |
fi | |
rm $1 | |
mv $1.tmp $1 | |
} | |
for d in $1/*; do | |
utf8bom $d | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment