Unix channel operators make the Input Source ineffective

Source: Internet
Author: User
Article Title: unix channel operators make the Input Source ineffective. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

In the Unxi operating system, standard input and standard output are two independent streams that can be used separately in the shell program. However, sometimes System Engineers need to disable some specific input sources. It is often used by system engineers when developing some practical script programs.

  1. When do I need to disable the input source?

When should I disable the input source? This is not a question. Here are two simple examples.

First, it must be used when calculating the file size. For example, if System Engineers need to calculate the size of all text files in the current directory, wc? C *. txt command. With this command, the operating system will display all text files and their corresponding sizes in the current directory, and then count the total size of the text files in the last line. However, when writing some script programs, you may only need to use the total size of the file, instead of the size of each file. For example, after an Oracle database is deployed on a Unix operating system, some log documents are generated. The database administrator needs to regularly clean up these logs for security and other considerations. At this time, the database administrator may ask the Unix system engineer to ask the operating system to count the size of these log files every day, when the capacity of these files reaches a certain value, the database administrator is automatically notified to process the files. Because this script involves a logic judgment problem, you only need to have the total size of the final file in the returned results. If there are details and total sizes, the logic operator parameters cannot be used. In this case, some input sources must not work. There are many similar cases. For example, when writing a disk quota management tool, you need to calculate the total size of the target file before downloading and copying files from the network or elsewhere. If the total size of the target file is smaller than the available space, the target file will be rejected directly, instead of being told to the user when the file cannot be placed. This will waste users' time. In this case, you only need to use the total size of a file.

 Second, you need to filter some error information input streams.

For example, when you use the find command to search for files, if the current user of the directory to be searched does not have relevant permissions, the system prompts some error messages. However, if the user's permissions are relatively small and the user searches for the entire operating system, there will be more error messages, which will affect the user's reading. In this case, you also need to disable the input stream of these error messages.

To sum up, the input source must not work in many places. So how does one achieve this goal in a Unix operating system?

3. How can I disable the input source through MPs queue?

In Unix operating systems, you can use MPs queues to disable some input sources. Pipelines in Unix operating systems refer to the third source and target of standard input and standard output. In short, the pipeline operator can combine the standard input stream and standard output stream, or use the standard output stream of a command as the standard input stream of another command. This pipeline operator can be used in combination with other commands to achieve complicated requirements.

If the system engineer wants to measure the size of text files in the Oracle database directory, the system engineer can switch to the Oracle database directory and then use cat *. txt | wc? C command. This command is to combine the two basic commands through the pipeline to display the total file size in the operating system. Then how does this command implement this function?

First, the cat *. txt command merges all txt files in the current directory. The Cat command is mainly used to read the file content. This command can open multiple files at the same time. If multiple files are opened at the same time, the cat command merges the contents of the files. Add the content of the second file to the end of the first file. No prompt will be added in the middle. Although this may not be easy to read, it is the most authentic reflection of the content of the file itself, without any modification.

The role of the pipeline operator is to regard the standard output stream of the cat command as a standard input stream of the wc command. The wc command and the-c option are used to calculate the size of the standard input stream. All this command can calculate the size of all txt files in the Oracle directory, rather than the size of each file. That is to say, it has reached the goal of making a system input source ineffective. After this value is obtained, the system engineer can use the IF statement or other commands to perform some specific purposes.

ThuWhat are precautions for using pipeline operators?

Pipeline operator is an important tool in the Unix operating system and a major improvement not available in the Windwos operating system. As we all know, when designing Unix operating systems, they follow the design principles of small or useless. Each command only provides some simple functions. In the preceding example, the Unix operating system designer can add a parameter to the wc command so that the wc command only displays the final result without showing details. But the designers did not. If this is the case, you need to add this option to many commands. That destroys the small Unix operating system, that is, the beautiful design principle. For this reason, the Unix system designer promotes system engineers to combine multiple streams using pipeline operators, so that the output of one command can be used as the input of another command to realize the combination of commands. Therefore, pipeline operators are the core of Unix operating systems. Basically, every practical script program can see the existence of pipeline characters. As a system engineer, pipeline operators must be used. Based on my experience, pipeline operators are theoretically simple. In any textbook, the introduction of pipeline operators is estimated to be no more than five pages. However, the contents of MPs queues are much larger than those mentioned in these books. That is to say, System Engineers must use good channel operators, mainly relying on the accumulation of experience in future work. To say something more philosophical, it means that the application of pipeline operators can only be said or not. Therefore, I think the application effect of pipeline operators is directly related to the work experience of System Engineers. For this reason, I suggest that if you want to improve your understanding of the channel operators, the best way is to look at some script programs written by people. From these script programs, System Engineers can have a lot of GAINS. I often read the programs compiled by some dinosaur experts, and often learn some experience in pipeline operators. In short, there are basically no advanced techniques for pipeline operators in books. To become a master of Pipeline character applications, it is a shortcut to capture programs written by observation experts.

In addition, I will introduce you to the basic rules for pipeline operators. Mastering this rule is useful for everyone to observe programs written by others or when they use pipeline operators. This rule means that the command on the left of the pipeline operator must use standard output, while the command on the right must use standard input. That is to say, the System Engineer saw a complicated command consisting of five or six pipeline operators. In this case, you don't have to worry about it. You just need to read it from left to right. The previous command is a standard output, and the output result is the input parameter of the next command, and so on.

Pipeline operators can be used to combine some simple commands to implement complex functions. For this reason, some experts also compared the image of Unix operating systems from building block games. Small blocks are Unix operating system commands. If these blocks are combined into a good-looking model toy (responsible function) through plug-ins (pipeline symbols ), this not only requires users to have a solid basic knowledge and understand the usage of each command, but also requires system engineers to have a certain degree of imagination. Only with imagination can we combine commands that are not related to each other to complete some complex functions. I'm afraid this is also a special requirement of the Unix operating system for System Engineers. Other development languages or operating systems do not have such strong requirements as Unix operating systems. For this reason, System Engineers must be creative and dare to try pipeline operators.

For this reason, I believe that when using pipeline operators, System Engineers should be good at learning from other experts' programs, be good at imagination, and follow the basic rules of pipeline operators. Only in this way can System Engineers become experts at the application layer of pipeline operators.

  4. Are there any other solutions?

You can use MPs queues to disable some input sources. However, a special file is provided in the Unix operating system, which can also be used for filtering. This file is the/dev/null file. This is a special file in a Unix operating system. It receives input from any stream but does not increase the file size. However, the purpose of this file is far from the role of pipeline operators. In general, it is appropriate to use this file if it is used to filter some standard error information. In other cases, if the input source does not work, the author suggests that System Engineers use pipeline operators.


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.