From a84b10645afc0fcd17360587f7dbfae2cf07462a Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Wed, 17 Jan 2018 14:25:03 +0800 Subject: Add unix/text-to-utf8.sh --- unix/text-to-utf8.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 unix/text-to-utf8.sh (limited to 'unix') 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##*/} ..." + 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 -- cgit v1.2.2