In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how escapes and references are used to manage metacharacters in Unix. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
In the Unix operating system, there is a special class of characters called metacharacters. They have a special meaning in the system. Such as * and? The operating system considers these metacharacters to be wildcards. If there are these metacharacters in the path, file name, or command parameters, it will cause misunderstanding in the operating system. For this reason, some methods should be adopted in the system to enable the operating system to treat these metacharacters as ordinary characters. In the Unix system, escape and reference are used to deal with it. What the systems engineer needs to understand is the difference between the two solutions. And in specific circumstances, the use of appropriate solutions.
First, the application of escape function.
In fact, the escape mechanism exists not only in the Unix operating system, but also in other programming languages. If you have experience in program development, it may be easier to understand this escape function. To put it simply, the escape function refers to adding an escape character\ before some metacharacters with special meaning, in order to tell the operating system that this is a common character and cancel the special meaning of metacharacters. If * originally stands for wildcard characters, such as ls *, it means all files and directories. The\ * sign is treated as an ordinary character and the system will no longer think of it as a wildcard.
The common application of this escape function is relatively simple, just precede the metacharacter with\. However, the author also needs to emphasize several special applications of escape characters. Although these applications are special, ordinary users don't usually use them. But for systems engineers, these features can be particularly useful.
First, you need to insert a space in the file name. If there is a My Documents folder in Microsoft operating system, there is a space in the middle. This space also belongs to a special original character in the Unix system. If you add spaces directly when creating a file or directory, the Unix operating system will prompt for an error message. The help of transferring characters is often needed at this time. If you use the command mkdir My\ Documents, you can create a directory name with a space between it. The same is true if you want to create a file name with spaces. However, it should be noted that if metacharacters are included in a file or directory, you also need to use transfer characters to define these special metacharacters when using the delete command or directory location command. Otherwise, some inexplicable problems may arise.
The second is to wrap the command with escape characters. Some commands in the Unix operating system are particularly complex, especially those subordinate to the Unix operating system. For example, expdp is a database object export command in Oracle database. Although this command is very powerful, it is a pity that some specific functions can only be accomplished through complex parameters. Sometimes it takes several lines to write a command. Although the Unix operating system provides a way to automatically wrap commands. However, the function of automatic line wrapping is relatively weak, for example, it will not be done where the system engineering wants to change the line. For this reason, using this automatic line wrapping will make it difficult for the code to read. So many system engineers still hope to be able to do their own manual branches, branches after some key parameters, in order to increase the readability of the command. However, if you directly use the enter key to branch, the system is not recognized. Because the enter key system is considered to be a key to execute a command. That is, when the system engineer clicks the enter key, the system will think that the user has entered the command. After typing the enter key, the system runs this command automatically. So typing enter directly often does not fulfill the requirements of the command branch. Help with escaping characters is needed at this point. If the existing command is long, the engineer wants to be able to split it into two lines, mainly by adding another line after the parameter-name. At this point, you can add an escape character\ before this parameter, and then press enter. Because of this escape character, the system cancels the newline effect of the enter key. After doing so, a secondary prompt appears, which indicates that the command is not finished, and the next line continues. This feature can be very useful for systems engineers. Because at this time, the system engineer can easily branch a long list of commands according to their own needs, so as to improve the readability of the commands.
It is also important to note that the escape character itself is a special metacharacter. Users also need to use transfer characters if they want to use this\ symbol in a command or file name. If the user wants to use the echo or printf command to display the URL. There are a lot of\ symbols in the URL, so you need to use escape characters to let the system treat the\ symbols as ordinary characters.
Second, use references to solve the problem of metacharacters.
In addition to using the reference function mentioned above to deal with these metacharacters, you can also use the reference function to solve the problem. To put it simply, if you place a command parameter within a pair of quotation marks, if there are metacharacters in the quotation marks, these metacharacters will not work. So now that the escape function can solve the problem of metacharacters, the system also proposes a referenced solution, is it multiple times? Actually this is not so. When a command line contains multiple metacharacters, you need to precede each metacharacter with a transfer character. Therefore, it will be very tedious to use escape characters to solve the problem of metacharacters at this time. At this time, if the reference mechanism is used to solve the problem of metacharacters, it may be more ideal. For example, now the system engineer wants to be able to type the following message on the screen (this is the path to a shared file): 192.128.11.3\ share\ IT\ software\ pdf. If the transfer character is used, how to write it? Because there are four metacharacters (escape character\) in this output, the administrator has to deal with it with four transfer characters. It should be written as echo 192.128.11.3\\ share\\ IT\\ software\\ pdf. This is obviously very troublesome. In this case, it would obviously be more appropriate to use a reference. If you use the reference mechanism, you only need to write this command as:
Echo '192.128.11.3\ share\ IT\ software\ pdf'
Put a long list of commands in single quotation marks. Some metacharacters on the command line are treated as normal characters. That is, there is no need to use transfer characters for each metacharacter. Obviously this reference solution is much more convenient than using escape characters to solve the problem.
When using the reference mechanism to deal with metacharacters, you need to pay attention to the difference from double quotes. For example, there are three commands, echo $JAVA_HOME, echo'$JAVA_HOME', and "echo $JAVA_HOME". Where $JAVA_HOME represents the environment variable of the application Java. What will happen if the system engineer runs the above three commands in turn? the * command will display the environment variable of Java normally; the second command will display $JAVA_HOME directly, which means that the metacharacter $has been treated as a normal character. The third command still displays the environment variable of Java. It can be seen that double quotation marks and single quotation marks are different in citation mechanism. So what's the difference between them? In general, the systems engineer needs to pay attention to the following. Single quotation marks protect all metacharacters, that is, when metacharacters are encountered, they will tell the system to treat them as normal characters. However, if double quotation marks are used, the system interprets the content between the single quotation marks as the command line. For example, $will be used as the prefix of the environment variable and so on. In fact, single quotation marks and double quotation marks also have a function of mutual protection. That is, double quotes protect the single quotation marks, and single quotation marks protect the double quotation marks. Because both double quotation marks and single quotation marks are metacharacters themselves, they can be protected by the reference mechanism. However, in the case of both single quotation marks and double quotation marks (both as ordinary characters), the author suggests that it is better to use transfer characters to protect single or double quotation marks. This can avoid misunderstandings and improve the readability of the code. Similarly, if you want to use the transfer character\ as an ordinary character, then * also uses single quotation marks to protect the transfer character, rather than using the transfer character to protect the transfer character. Although these do not affect the practical application, they are effective means to improve the readability of the code.
As can be seen from the above analysis, metacharacters can be treated as ordinary characters, although transfer characters and reference mechanisms can be treated as ordinary characters. However, there are still some differences in the implementation methods between the two. To this end, it is possible to choose the appropriate solution according to the different applications. Generally speaking, the difference between the two is only the difference in the implementation method, and there is no difference in the specific function. However, for the sake of the readability of the code, you still need to be careful in choosing a specific solution. In general, however, the systems engineer must master both methods. And then according to different circumstances, now the appropriate solution. If you master one solution alone, you may not be able to solve all the problems related to metacharacters.
Thank you for reading! This is the end of the article on "how to manage metacharacters by escaping and quoting in Unix". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.