aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2018-01-17 14:25:03 +0800
committerAaron LI <aly@aaronly.me>2018-01-17 14:25:03 +0800
commita84b10645afc0fcd17360587f7dbfae2cf07462a (patch)
tree2a27592a3ee12884eb3511be314eb94e9daf47cc
parent03ced730d2fafd35511e9a114b7d7e04aa9b8c75 (diff)
downloadatoolbox-a84b10645afc0fcd17360587f7dbfae2cf07462a.tar.bz2
Add unix/text-to-utf8.sh
-rwxr-xr-xunix/text-to-utf8.sh40
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