Use escape and reference in Unix to manage metacharacters (1)

Source: Internet
Author: User

In Unix, there is a special type of character called metacharacters. They indicate special meanings in the system. For example, * and? The operating system considers these metacharacters as wildcards. If these metacharacters exist in the path, file name, or command parameters, the operating system may be misunderstood. Therefore, some methods should be used in the system to enable the operating system to treat these metacharacters as common characters. In Unix systems, the conversion and reference methods are used for processing. What System Engineers need to understand is the difference between the two solutions. And adopt appropriate solutions under specific circumstances.

1. Application of the escape function.

In fact, the escape mechanism is not only available on Unix operating systems, but also similar processing mechanisms in other programming languages. If you have experience in program development, this escape function may be easier to understand. Simply put, the escape function is to add an escape character \ before some metacharacters with special meanings to tell the operating system that this is a common character, cancels the special meaning of metacharacters. For example, if * represents a wildcard, or if ls * represents all files and directories. The \ * is to treat the * as a common character, and the system will no longer regard it as a wildcard.

The common application of this escape function is relatively simple. You only need to add \ before the metacharacters. However, the author also needs to emphasize several special applications of escape characters. Although these applications are special, they are rarely used by common users. However, these functions may be particularly useful for System Engineers.

First, insert a space in the file name. For example, if there is a My Documents folder in the Microsoft operating system, there is a space in the middle. This space also belongs to a special original character in Unix systems. If spaces are directly added when a file or directory is created, the Unix operating system will prompt an error message. At this time, it is often necessary to transfer the help of characters. For example, run the command mkdir My \ Documents ents to create a directory name with spaces in the middle. This is also true if you want to create a file name with spaces. However, if a file or directory contains metacharacters, you also need to use transfer characters to define these special metacharacters. Otherwise, some inexplicable problems may occur.

Second, use escape characters to wrap the command. In Unix operating systems, some commands are especially complex, especially those that are subordinate to Unix operating systems. For example, expdp is a database object Export command in the Oracle database. Although this command has powerful functions, it is a pity that only complex parameters can be used to complete some specific functions. Sometimes it takes several lines to write a command. Although the Unix operating system provides the command line feed method. However, the automatic line feed function is weak. For example, the line feed is not required in the system project. Therefore, using this automatic line feed will make it difficult to read the code. As a result, many System Engineers still want to manually execute branches and branch them after some key parameters to increase the readability of commands. However, if you directly use the return key branch, the system does not recognize it. This is because the return key system considers it as a key for executing commands. That is, when the system engineer clicks the Enter key, the system will think that the user has entered the command. After you type the Enter key, the system automatically runs this command. Therefore, directly entering the Enter key cannot meet the requirements of the command branch. In this case, the help of escape characters is required. If an existing command is long, the engineer wants to divide it into two lines, mainly by dividing the content after the parameter-name into another line. Now you can add an escape character \ before this parameter, and then press the Enter key. With this escape character, the system will cancel the line feed function of the carriage return key. After this operation, a secondary prompt will appear, indicating that the command has not ended and the next line continues. This function may be very practical for System Engineers. At this time, System Engineers can easily split a long string of commands according to their own needs, thus improving the readability of the commands.

Note that the Escape Character \ is a special metacharacter. If you want to use this \ symbol in the command or file name, you also need to use the transfer character. If you want to use the echo or printf command to display the URL. There are many \ symbols in the website. In this case, escape characters must be used for the system to treat the \ symbol as a common character.

2. Use references to solve metacharacters.

In addition to the reference function mentioned above, you can also use the reference function to process these metacharacters. Simply put, a command parameter is placed in a pair of quotation marks. If the content in the quotation marks contains metacharacters, these metacharacters will not work. Now that the escape function can solve the problem of metacharacters, the system also proposes a reference solution. Do you want to do this multiple times? Actually not. When a command line contains multiple metacharacters, you must add an escape character before each metacharacters. To solve this problem, it is very complicated to use escape characters. In this case, it may be more ideal to use the reference mechanism to solve the problem of metacharacters. For example, the system engineer wants to output the following information on the screen (this is the path to a shared file): 192.128.11.3 \ share \ IT \ software \ pdf. If transfer characters are used, what should I do? The output contains four metacharacters (Escape Character \). Therefore, the Administrator has to use four escape characters for processing. Echo 192.128.11.3 \ share \ IT \ software \ pdf is required. This is obviously very troublesome. In this case, it is obviously more appropriate to use references. To use the reference mechanism, you only need to write this command as follows:

Echo '2017. 128.11.3 \ share \ IT \ software \ pdf'

Enclose a large string of commands in single quotes. The system treats some metacharacters in the command line as common characters. That is, you do not need to use transfer characters for each metacharacter. Obviously, this reference solution is much more convenient than using escape characters.



Related Article

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.