Skip to content

Instantly share code, notes, and snippets.

@MrWu94
Created November 15, 2016 08:39
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 MrWu94/5c65a327eb029cc9abf28f3a6a604934 to your computer and use it in GitHub Desktop.
Save MrWu94/5c65a327eb029cc9abf28f3a6a604934 to your computer and use it in GitHub Desktop.
#!/bin/bash
#####################################################
# NAME
# gbk2utf8.sh
# conv file encoding form gbk to utf-8 or reverse
# USAGE
# ./gbk2utf8.sh FILENAME
# ./gbk2utf8.sh -r FILENAME
#####################################################
if [ $# -eq 0 ];then
echo "need filename for argument."
exit 1;
fi
if [ "$1" == "-r" ];then
# echo "-r option"
[ $# -eq 1 ] && echo "need filename." && exit 1;
filename=$2
sourcecode="UTF-8"
descode="GBK"
else
# echo "default option."
filename=$1
sourcecode="GBK"
descode="UTF-8"
fi
if [ -f ${filename} ];then
echo "file name: ${filename} ${sourcecode} to ${descode}"
echo "iconv -f ${sourcecode} -t ${descode} ${filename} -o \"${filename}.out\" >>/dev/null"
iconv -f ${sourcecode} -t ${descode} ${filename} -o "${filename}.out" >>/dev/null
mv "${filename}.out" ${filename}
echo "conv file ${filename} form ${sourcecode} to ${descode} succes."
else
echo "file: ${filename} does not exists."
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment