解決在LINUX下中文亂碼的問題,供參考

我在《Linux環境打開Windows下編寫的中文文檔亂碼問題》一文是提到在linux環境下用可以用mousepad或leafpad文本閱讀器打開windows下文本解決中文亂碼。博友inbi評論中說有”有unix2dos和dos2unix這兩個命令“。首先,我在上一博文中要解決的問題是在LINUX環境下正常閱讀Windows下編寫的含有中文字符的文本,而不對文本進行寫操作(即改變文本格式:比如格式轉換、屬性、權限等操作),在這裏,我也提醒各位做LINUX系統管理的朋友,在LINUX系統環境下,在對任何文件進行寫操作時都儘量保持審慎態度(比如一個業務數據文件的Modify屬性的改變就有可能導致基於此屬性的數據備份任務失敗)。其次,unix2dos和dos2unix這兩個命令是用來轉換文本斷行格式的,而無法對文本的字符編碼進行轉換。下面,我就linux下文本字符編碼轉換和斷行字符轉換給大家講解。

接下來,我們依舊使用《Linux環境打開Windows下編寫的中文文檔亂碼問題》一文中的afaty@afaty-laptop ~/Downloads/system_status/Readme.txt進行演示。

我們先看一下,這個文件還是原來那個文檔

afaty@afaty-laptop ~/Downloads/system_status $ cat Readme.txt # 沒錯,還是那個文件,依舊亂碼。
����ʹ�
��������ѹ������Ҫ�������κ�UNIX/Linuxϵͳ��,Ȼ��ִ�����еĽű��ļ����
#./cpu.sh
disk.sh��ʹ����Ҫ��������,��/dev/sda1�
��һ����ҪrootȨ�
Ŀǰ֧�ֵ�ϵͳƽ̨:Linux,AIX,Solaris,HP-UX,SCO-UNIX

一、文本字符編碼轉換

第一種方法:使用enca命令進行轉換。

這個工具一般LINUX發行版默認沒有安裝,如果你要使用需要手動安裝。

afaty@afaty-laptop ~/Downloads/system_status $ file -bi Readme.txt  #我們先看一下這個文本的編碼格式。
text/plain; charset=iso-8859-1   # file命令太笨了,竟然猜錯了……不可能是iso-8859-1的,我們試試別的工具。
afaty@afaty-laptop ~/Downloads/system_status $ enca -L zh_CN Readme.txt  # enca這個工具還是聰明些。不過我們還是需要給它指定語言(zh_CN)。
Simplified Chinese National Standard; GB2312 
afaty@afaty-laptop ~/Downloads/system_status $ cp Readme.txt Readme.txt.GB2312 # 我們先拷貝一份文件用來實驗。
afaty@afaty-laptop ~/Downloads/system_status $ enca -L zh_CN -x UTF-8 Readme.txt.GB2312 # 轉化爲UTF-8格式,這裏在原文件上直接轉化了。
afaty@afaty-laptop ~/Downloads/system_status $ cat Readme.txt.GB2312 # 中文正常顯示了。除了enca,我們還可以用iconv這個命令來實現,下面看我實驗。
如何使用
將程序解壓到你需要監測的任何UNIX/Linux系統中,然後執行其中的腳本文件即可
#./cpu.sh
disk.sh的使用需要給出參數,如/dev/sda1等
不一定需要root權限
目前支持的系統平臺:Linux,AIX,Solaris,HP-UX,SCO-UNIX
# 爲了不改變原文件,我們可以用以下方式轉換。
afaty@afaty-laptop ~/Downloads/system_status $ rm -rf Readme.txt.GB2312 
afaty@afaty-laptop ~/Downloads/system_status $ cp Readme.txt Readme.txt.GB2312
afaty@afaty-laptop ~/Downloads/system_status $ enca -L zh_CN -x UTF-8 < Readme.txt.GB2312 > Readme.txt.UTF-8  存入Readme.txt.UTF-8文件中。
afaty@afaty-laptop ~/Downloads/system_status $ cat Readme.txt.UTF-8 
如何使用
將程序解壓到你需要監測的任何UNIX/Linux系統中,然後執行其中的腳本文件即可
#./cpu.sh
disk.sh的使用需要給出參數,如/dev/sda1等
不一定需要root權限
目前支持的系統平臺:Linux,AIX,Solaris,HP-UX,SCO-UNIX

第二種方法:使用iconv命令進行轉換。

這個工具一般LINUX發行版默認已經安裝,可以直接使用。

afaty@afaty-laptop ~/Downloads/system_status $ rm -rf Readme.txt.GB2312
afaty@afaty-laptop ~/Downloads/system_status $ rm -rf Readme.txt.UTF-8 
afaty@afaty-laptop ~/Downloads/system_status $ cp Readme.txt Readme.txt.GB2312
afaty@afaty-laptop ~/Downloads/system_status $ iconv -f GB2312 -t UTF-8 < Readme.txt.GB2312 
如何使用
將程序解壓到你需要監測的任何UNIX/Linux系統中,然後執行其中的腳本文件即可
#./cpu.sh
disk.sh的使用需要給出參數,如/dev/sda1等
不一定需要root權限
目前支持的系統平臺:Linux,AIX,Solaris,HP-UX,SCO-UN
afaty@afaty-laptop ~/Downloads/system_status $ iconv -f GB2312 -t UTF-8 < Readme.txt.GB2312 > Readme.txt.UTF8 #保存到Readme.txt.UTF8文件中。
afaty@afaty-laptop ~/Downloads/system_status $ cat Readme.txt.UTF-8 
如何使用
將程序解壓到你需要監測的任何UNIX/Linux系統中,然後執行其中的腳本文件即可
#./cpu.sh
disk.sh的使用需要給出參數,如/dev/sda1等
不一定需要root權限
目前支持的系統平臺:Linux,AIX,Solaris,HP-UX,SCO-UNIX

二、文本斷行字符轉換

這裏我們主要討論如何將Windows下編寫的腳本的斷行格式轉化爲LINUX下腳本斷行格式。我們在現在項目中經常遇到,我們的程序寫的沒有任何問題,但就是無法正常執行,一般都是因爲不同操作系統平臺下編寫程序而導致程序中的斷行格式不同造成的。下面我們看一個實例。我用bash在windows和linux下分別編寫了一個Hello,World程序,程序中寫了一樣的東西。程序名分別爲hello-win.sh和hello-linux.sh。下面我們用diff工具看一下。

afaty@afaty-laptop /tmp $ diff hello-win.sh hello-linux.sh 
1,5c1,5
< #!/bin/bash
< #  filename: hello-win.sh
< #  It is my first bash script.
< 
< echo "Hello, World!"
---
> #!/bin/bash
> #  filename: hello-linx.sh
> #  It is my first bash script.
> 
> echo "Hello, World."

從上面的代碼中好像看不出來什麼哦,但問題卻是實實在在的,估計多數碼農都是被類似問題弄瘋的。下面我們嘗試執行一下這兩個腳本看看。

afaty@afaty-laptop /tmp $ chmod +x hello-win.sh hello-linux.sh # 先給兩個程序加執行權限。
afaty@afaty-laptop /tmp $ ./hello-win.sh 
bash: ./hello-win.sh: /bin/bash^M: bad interpreter: No such file or directory
afaty@afaty-laptop /tmp $ ./hello-linux.sh 
Hello, World.
afaty@afaty-laptop /tmp $

我們可以看到,上面兩個腳本執行的結果:hello-win.sh執行錯誤,hello-linux.sh沒問題。那我們用別的工具看看,看兩個文件到底有什麼不一樣。

afaty@afaty-laptop /tmp $ cat -A hello-win.sh hello-linux.sh
#!/bin/bash^M$
#  filename: hello-win.sh^M$
#  It is my first bash script.^M$
^M$
echo "Hello, World!"^M$
#!/bin/bash$
#  filename: hello-linx.sh$
#  It is my first bash script.$
$
echo "Hello, World."$

從以上代碼中我們可以看到,hello-win.sh文件中的換行符爲“^M$”,而hello-linux.sh文件中的換行符是”$”。問題就在這裏了。現在,我們用inbi兄提到的dos2unix命令轉換一下hello-win.sh文件中的換行符看看。
第一種方法:使用dos2unix

afaty@afaty-laptop /tmp $ cp hello-win.sh hello-win.sh.orig
afaty@afaty-laptop /tmp $ dos2unix hello-win.sh
dos2unix: converting file hello-win.sh to UNIX format ...
afaty@afaty-laptop /tmp $ cat -A hello-win.sh
#!/bin/bash$
#  filename: hello-win.sh$
#  It is my first bash script.$
$
echo "Hello, World!"$
afaty@afaty-laptop /tmp $ ./hello-win.sh 
Hello, World!

第二種方法:使用sed命令
我們看到,hello-win.sh和hello-linux.sh兩個文件的區別主要在於hello-win.sh的換行符多了個“^M”,那麼從理論上來講,我們只要把“^M”控制字符刪掉即可。下面我們實驗驗證。

afaty@afaty-laptop /tmp $ cp hello-win.sh.orig hello-win.sh
afaty@afaty-laptop /tmp $ cat -A hello-win.sh
#!/bin/bash^M$
#  filename: hello-win.sh^M$
#  It is my first bash script.^M$
^M$
echo "Hello, World!"^M$
afaty@afaty-laptop /tmp $ sed -i 's/^M//g' hello-win.sh
afaty@afaty-laptop /tmp $ cat -A hello-win.sh
#!/bin/bash$
#  filename: hello-win.sh$
#  It is my first bash script.$
$
echo "Hello, World!"$
afaty@afaty-laptop /tmp $ chmod +x hello-win.sh
afaty@afaty-laptop /tmp $ ./hello-win.sh 
Hello, World!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章