diff options
Diffstat (limited to 'unix')
-rwxr-xr-x | unix/text-to-utf8.sh | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/unix/text-to-utf8.sh b/unix/text-to-utf8.sh new file mode 100755 index 0000000..b48a6c7 --- /dev/null +++ b/unix/text-to-utf8.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# +# Convert text file to UTF-8 encoding. +# +# Aaron LI +# 2018-01-17 +# + +case "$1" in + ""|-h|--help) + echo "usage: ${0##*/} <file1> ..." + exit 1; + ;; +esac + +convert() { + from="$1" + infile="$2" + iconv -f $from -t UTF-8 "$infile" > _tmp.$$ + if [ $? -eq 0 ]; then + mv _tmp.$$ "$infile" + echo "converted to UTF-8 from $from" + else + rm _tmp.$$ + echo "ERROR: failed conversion from $from" + fi +} + +while [ -n "$1" ]; do + infile="$1" + shift; + echo "=== $infile ===" + if file "$infile" | grep -q "UTF-8 Unicode"; then + echo "already UTF-8; skip it" + elif file "$infile" | grep -q "UTF-16 Unicode"; then + convert UTF-16 "$infile" + else + convert GBK "$infile" + fi +done |