shell脚本中使用iconv转换字符编码的问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
poplarc
帖子: 43
注册时间: 2007-01-20 22:20
来自: 深圳

shell脚本中使用iconv转换字符编码的问题

#1

帖子 poplarc » 2008-03-06 15:08

我的终端字符编码是UTF-8,因工作需要经常从telnet登录到另外一台使用GBK编码的机器。
为了防止登录后终端显示乱码,每次登录前都要手工将编码改为GBK,并在退出登录之后切换回UTF-8,感觉很烦。
请问能否同过脚本完成上面一行的两项工作?
在论坛上搜索到一个介绍iconv做类似工作的贴子,由于不太懂shell脚本没看明白。
哪位大侠帮我详细讲解一下吧。谢谢! :oops:
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#2

帖子 eexpress » 2008-03-06 15:47

# 如果不是UTF-8的标题,就转换一下。
[ $? != 0 ] && t=`echo $t|iconv -f GBK -t utf8|tr -d \n`
t=`echo $t|sed 's/|/_/g'`
● 鸣学
头像
poplarc
帖子: 43
注册时间: 2007-01-20 22:20
来自: 深圳

#3

帖子 poplarc » 2008-03-06 16:03

eexpress 写了:# 如果不是UTF-8的标题,就转换一下。
[ $? != 0 ] && t=`echo $t|iconv -f GBK -t utf8|tr -d \n`
t=`echo $t|sed 's/|/_/g'`
eexpress你好。
如果在登录之前之前将终端编码改成GBK的话,登录之后会在终端上显示如下界面。
若没有改成GBK则其中的中文就都显示为乱码。
我根据你提供的代码写了如下脚本:

#!/bin/bash
# 如果不是UTF-8的标题,就转换一下。
[ $? != 0 ] && t=`echo $t|iconv -f GBK -t utf8|tr -d \n`
t=`echo $t|sed 's/|/_/g'`
telnet 192.168.xx.xx

这样还是不能正常显示中文,请问我这个改法是不是有问题啊。多谢。

┌─────────────
│ 1. xx菜单 |
│ 2. |
│ 3. xx报告菜单 |
│ 4. |
│ 5. xx调整菜单 |
│ 6. |
│ 7. xx菜单 |
│ 8. |
│ 9. xx菜单 |
│10. |
└─────────────
┌────────────-
│Please select a function. |
└────────────
chasye
帖子: 333
注册时间: 2007-05-13 15:20

#4

帖子 chasye » 2008-03-06 16:54

export LANG=zh_CN.GBK telnet youhost
头像
poplarc
帖子: 43
注册时间: 2007-01-20 22:20
来自: 深圳

#5

帖子 poplarc » 2008-03-07 8:50

chasye 写了:export LANG=zh_CN.GBK telnet youhost
chasye你好。
按照你提供的方法实验了一下,屏幕还是会显示成乱码。
另外export和telnet同时写在一行里是需要用;隔开的。
chasye
帖子: 333
注册时间: 2007-05-13 15:20

#6

帖子 chasye » 2008-03-07 9:07

LANG=zh_CN.GBK telnet youhost
试试 上面的
头像
poplarc
帖子: 43
注册时间: 2007-01-20 22:20
来自: 深圳

#7

帖子 poplarc » 2008-03-07 17:23

chasye 写了:LANG=zh_CN.GBK telnet youhost
试试 上面的
chasye你好,我先后尝试了:
1) LANG=zh_CN.GBK telnet host
2) env LANG=zh_CN.GBK telnet host
3) env LC_ALL=zh_CN.GBK telnet host
都不能达到转换终端编码的目的,困惑中 :?
chasye
帖子: 333
注册时间: 2007-05-13 15:20

#8

帖子 chasye » 2008-03-07 17:57

poplarc 写了:
chasye 写了:LANG=zh_CN.GBK telnet youhost
试试 上面的
chasye你好,我先后尝试了:
1) LANG=zh_CN.GBK telnet host
2) env LANG=zh_CN.GBK telnet host
3) env LC_ALL=zh_CN.GBK telnet host
都不能达到转换终端编码的目的,困惑中 :?
你的locale里有没有zh_CN.GBK啊???

没有就先,试试
添加zh_CN.GBK到locale
再local-gen
再用上面的命令
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#9

帖子 eexpress » 2008-03-07 19:57

☎ alias |grep LC
alias cn='export LC_ALL=zh_CN.UTF-8'
alias en='export LC_ALL=C'
我一般是这样,重来不去改LANG
cn; ls
编码你自己根据locale修改所需的。
● 鸣学
头像
poplarc
帖子: 43
注册时间: 2007-01-20 22:20
来自: 深圳

#10

帖子 poplarc » 2008-03-10 16:59

chasye, eexpress, 二位好:

我又试了几次,可还是没达到预期效果。
我不太明白系统locale设置和终端编码之间有怎样的联系,
因为我在转换终端编码之前和之后分别查看了一次系统locale设置,结果发现转换编码前后的locale并无不同。
那为什么在终端中直接转换编码之后就不会显示为乱码呢?困惑中……

另外我也在网上搜索了部分有关locale设置的文章,并参照那些文章尝试着进行了一些设置,
目前系统的locale如下:
poplarc@johnathan-dt:~/test/ch08/8.2.4$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=


另外/usr/lib/locales目录下的内容如下:
poplarc@johnathan-dt:/usr/lib/locale$ ll
total 88
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 csb_PL
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_AU.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_BW.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_CA.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_DK.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_GB.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_HK.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_IE.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_IN
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_NZ.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_PH.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_SG.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_US.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_ZA.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 en_ZW.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 12:18 fur_IT.utf8
drwxr-xr-x 3 root root 4096 2008-03-10 13:29 zh_CN.gb2312
drwxr-xr-x 3 root root 4096 2008-03-10 12:19 zh_CN.gbk
drwxr-xr-x 3 root root 4096 2008-03-07 10:27 zh_CN.utf8
drwxr-xr-x 3 root root 4096 2008-02-28 10:07 zh_HK.utf8
drwxr-xr-x 3 root root 4096 2008-02-28 10:07 zh_SG.utf8
drwxr-xr-x 3 root root 4096 2008-02-28 10:07 zh_TW.utf8
poplarc@johnathan-dt:/usr/lib/locale$



不知道是否有什么地方被我弄错了,请帮忙看一下吧。
另外有没有locale的详细介绍文章可以推荐?
:shock:
回复