cnless.sh:改良版lessで、GBK符号化またはUTF-8符号化を自動的に識別できます.
5190 ワード
#!/bin/bash
# : GBK less ( GBK UTF-8 )
#v0.1 LINUX , UTF-8 ,less UTF-8 , less GBK ,
# enca (enca , , , GBK ), UTF-8 ,
# ; GBK , UTF-8 , 。
#v0.2
#@todo less ?
res=$(lsof -a -p $$ -d0 2>&1)
data=
if echo "$res" | grep -q "pipe" ; then
data=$(cat -) #-
else
if [ $# -eq 0 ] ; then
echo "usage:$0 <file>"
exit 1
fi
if [ ! -e "$1" ] ; then
echo "$1 not exists"
exit 2
fi
if [ -d "$1" ] ; then
echo "$1 is a directory"
exit 3
fi
data=$(cat "$1")
fi
gbk=0
enc=$(echo "$data" | enca 2> /dev/null) # , stderr, /dev/null
# GBK
if [ ! $? -eq 0 ] ; then
gbk=1
elif echo $enc | grep -q GB ; then
gbk=1
fi
#
if [ $gbk -eq 1 ] ; then
data=$(echo "$data" | iconv -f gbk -t utf-8)
echo "$data" | less
else
echo $data | less
fi