Skip to content

Instantly share code, notes, and snippets.

@AndrewTsao
Created May 20, 2016 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewTsao/7da076d454f8066f0c885f79a460f1e0 to your computer and use it in GitHub Desktop.
Save AndrewTsao/7da076d454f8066f0c885f79a460f1e0 to your computer and use it in GitHub Desktop.
使用BASH对文本文件进行编码格式转换格
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