Skip to content

Instantly share code, notes, and snippets.

@wangchen
Last active January 28, 2016 11:10
Show Gist options
  • Save wangchen/7f289f1bdb9ddefc4c22 to your computer and use it in GitHub Desktop.
Save wangchen/7f289f1bdb9ddefc4c22 to your computer and use it in GitHub Desktop.
iconv all files from GBK to UTF-8
#!/bin/sh
SUFFIX="iconv-all-backup"
function convert()
{
n_total=0
n_error=0
while read f; do
fb="$f.$SUFFIX"
mv "$f" "$fb"
iconv "$@" "$fb" > "$f"
if [ $? -ne 0 ]; then
# Failed to convert, rollback
((n_error+=1))
cp "$fb" "$f"
fi
((n_total+=1))
done
echo "$n_total files proccessed, $n_error errors occurred."
}
function revert()
{
while read f; do
fb="$f.$SUFFIX"
if [ -e "$fb" ]; then mv "$fb" "$f"; fi
done
}
function clean()
{
while read f; do
rm -f "$f.$SUFFIX"
done
}
op="$1"
shift
case $op in
convert)
convert
;;
revert)
revert
;;
clean)
clean
;;
*)
echo "Usage: $0 {convert|revert|clean} iconv_args"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment