The Gnome Terminal,xshell and other terminal simulator executes the command to appear the garbled problem solves the __ garbled problem

Source: Internet
Author: User
Tags locale
First, the preface

Xshell compared to GNOME terminal, both are terminal simulators (in the Xshell can also perform simple built-in commands, such as "CD", "LS", etc.), the same status. Second, the principle analysis

When it comes to garbled code, you need to understand the encoding and decoding process. Execute the command in the terminal emulator, as shown in Figure 1.

Figure 1

In the above communication process, a series of encoding and decoding processes occur at the command execution unit. A series of encoding and decoding processes also occur at the terminal emulator, and we often create an SSH connection to establish a remote shell session in which the communication process diagram is shown in Figure 2. As Figure 2 shows, in this scenario, compared with the original communication process, only the "Command execution unit" and "terminal simulator" between the communication data through the SSH security channel, generally does not affect the overall encoding and decoding process.

Figure 2

A series of encoding and decoding processes occur during command execution. Different commands have different implementations, so the encoding and decoding processes that occur during the execution of different commands are not the same (the contents of these links can be used as evidence of this conclusion: "Using VI and less to view text appears garbled in Chinese, Use cat Normal, "View log file more normal, with less view appear garbled"). In addition, many commands will read the configuration values of the locale series environment variables (such as "LANG", "LANGUAGE", "Lc_all", "lc_ctype", etc.) during the execution to encode the decoding process.
For example, "uniq" command will use "lc_collate" and other environment variables configuration values, "grep" command to use "Lc_all" and other environment variables configuration values, "vim" command to use "LC_CTYPE" and other environment variables configuration values.

A series of encoding and decoding processes can also occur during the execution of a command in the terminal emulator, which has 3 main contents:
1. Configure the coding scheme used by the terminal simulator, for Gnome Terminal, in Figure 3 where the configuration, for the Xshell, in Figure 4 location for configuration, for Xftp, in Figure 5 location to configure
2, the implementation of the command, the use of Terminal Simulator configuration encoding scheme, the original command string encoding, the resulting stream of bytes passed to the "Command execution unit"
3, display the command execution result, obtains the word stream form the command execution result, uses the Terminal Simulator configuration The coding scheme, decodes it, obtains the string form the command execution result

Figure 3

Figure 4

Figure 5
third, the experiment 3.1, ls command 3.1.1, experiment 1

There is a file named "Hello. txt", and the storage content corresponding to that file name is stored with a byte stream encoded using the "UTF-8" encoding scheme.
Set different locale series environment variable configuration value and Terminal simulator coding scheme, get different execution result of "ls command", concrete as Table 1.

Table 1

Locale Series environment variable configuration value Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme ls command execution results screenshot
Zh_cn.utf8 UTF-8
Zh_cn.gbk UTF-8
Zh_cn.utf8 GBK
Zh_cn.gbk GBK
3.1.2, experiment 2

There is a file named "Hello. txt", and the storage content corresponding to that file name is stored with a byte stream encoded using the "GBK" encoding scheme.
Set different locale series environment variable configuration value and Terminal simulator coding scheme, get different execution result of "ls command", concrete as Table 2.

Table 2

Locale Series environment variable configuration value Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme ls command execution results screenshot
Zh_cn.utf8 UTF-8
Zh_cn.gbk UTF-8
Zh_cn.utf8 GBK
Zh_cn.gbk GBK
3.2, Vim command 3.2.1, experiment 1

There is an existing file called "A.txt" that stores content "How are you?" ", the encoding format used is" UTF-8 ".
Set up different locale series environment variable configuration value and Terminal simulator coding scheme, get different "vim a.txt" execution result, concrete as table 3.

Table 3

Locale Series environment variable configuration value (same as "3.1, ls command", set the configuration value of all environment variables in the locale series to the same value) Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme "vim a.txt" command execution results screenshot ": E ++enc=utf8" Results screenshot in VIM environment
Zh_cn.utf8 UTF-8
Zh_cn.gbk UTF-8
Zh_cn.utf8 GBK
Zh_cn.gbk GBK
3.2.2, Experiment 2

There is an existing file called "A.txt" that stores content "How are you?" ", the encoding format used is" GBK ".
Set up different locale series environment variable configuration value and Terminal simulator coding scheme, get different "vim a.txt" execution result, concrete as table 4.

Table 4

Locale Series environment variable configuration value (same as "3.1, ls command", set the configuration value of all environment variables in the locale series to the same value) Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme "vim a.txt" command execution results screenshot ": E ++enc=gbk" Results screenshot in VIM environment
Zh_cn.utf8 UTF-8
Zh_cn.gbk UTF-8
Zh_cn.utf8 GBK
Zh_cn.gbk GBK
Iv. Other 4.1. Locale Series environment variable

In the above experiments, the configuration values of all environment variables in the locale series are set to "ZH_CN.GBK" or "Zh_cn.utf8", in fact, "LS command implementation" or "VIM command implementation" only need to read the locale series of the specific parts of the configuration value of the environment variable, The configuration values of other environment variables in the locale series do not affect the operation of the LS command or the VIM command. It is only that we do not know what the "environment variables" of the specific parts of the locale series mentioned above are, so for the sake of simplicity, the configuration values of all environment variables in the locale series are set to either "ZH_CN.GBK" or "Zh_cn.utf8". 4.2, speculated that the VIM command read the contents of the main encoding and decoding process

From the experiment in the 3.2, Vim command, the main encoding and decoding process that is used to read the contents of the file using the VIM command is as follows: The byte stream to read the contents of the file, the encoding scheme specified using the locale series environment variable (also available through ": E ++enc=?"). Command yourself to specify the encoding scheme you want to use to decode the byte stream of the file content, and then use the locale series environment variable specified coding scheme for the character stream to code to get the word throttling, the aforementioned code to pass the byte stream to the terminal simulator.
Thus, as long as the encoding scheme specified by the locale series environment variable is consistent with the coding scheme used by the terminal simulator, the file content can be directly displayed or passed ": E ++enc=?" command to get the contents of the file is not garbled display. 4.3. Two scripts used in the experiment

Script 1:

#!/bin/bash 

Cd/home/dsl/tmp/shell

export lc_all=$1
export lang=$1
export language=$1

locale

echo "---------"
echo "---------"
echo "---------"

ls

Script 2:

#!/bin/bash Cd/home/dsl/tmp/shell export lc_all=$1 export lang=$1 export language=$1 locale echo "---------" echo "---------" echo "---------" Vim a.txt 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.