Article Title: What does Unix redirection bring to us. 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.
When using Unix systems, you may encounter the following problems:
1. Compile a large program in a Unix system. It may take 20 minutes. Some warning or error messages may appear during compilation. It is impossible for System Engineers to stare at the screen over the past 20 minutes. To this end, the system engineer hopes that shell can save the warning or error information during the compilation process in a file for later query by the system engineer or program developer.
2. During program development, sometimes the fee is charged based on the number of lines of code, as is the case for developing applications in Unix systems. For this reason, System Engineers also hope to have tools to conveniently count the number of lines in a program file.
3. Sometimes the system administrator may want to merge several text files. Similar functions can be achieved through copying and pasting. However, when many files need to be merged, it is not a good solution to achieve the goal of merging by assigning and pasting values. And so on.
Although these problems are not solved by specific commands in Unix systems. However, with the redirection function and some other processing skills, these problems can be solved.
1. Use the redirection function to merge files.
In Unix systems, the redirection function is particularly useful when multiple files are merged into one file. For example, the system administrator can use the redirection function to merge the A1.TXT and A2.TXT files (or even multiple files) in Unix systems. Before talking about the specific principles, I will write this command first.
Cat A1.TXT, A2.TXT> new.txt
The specific execution process of this command is as follows:
First, when the shell finds that the command line contains special characters, it will disconnect the stream from its default device. The system creates a new file new.txt to prepare for writing. However, pay attention to one problem. If the file already exists in the Unix system, the shell will overwrite the file without prompting the system administrator. Therefore, be cautious when using the output redirection function. In particular, it is best not to run this command as root. Privileged users have full control over all files and directories in the system. For this reason, if the file already exists, it will not hesitate to overwrite the file, even if the file owner is not a privileged user.
Shell then disconnects the default device. The default device of the Cat command is usually the screen terminal. However, when shell's command is interpreted, it can see that the command has a special symbol. In this case, the device will be disconnected and connected to the new.txt file. This file is used as its default display device. Note that the default device is disconnected and a new device is connected, instead of creating a backup device. In the hosts file.
Third, open the cat command, open the file in sequence, and read the data. Here, the system administrator needs to figure out whether shell has opened these two files or the command cat has opened these two files? Figuring out the answer to this question is helpful for future study. I will discuss this issue in subsequent articles. Here, the system administrator only needs to remember that when executing this command, the cat command opened these files instead of shell. Then the catcommand writes the read data to the standard output that has been redirected to the new.txt file.
Note that during the execution of this command, the Cat command does not know that it actually writes the content to a new file. That is to say, any command using standard output does not know the target object it outputs. They always think they are outputting data to the default device.
However, when we use the above command to merge files, there is a small defect. That is, the merged file name is not displayed in the preceding output stream merging process. The system administrator can expect that the name of the merged file can be displayed at the beginning of the new.txt file to improve the readability of the file. It is easy to implement this function. Assume that the current directory contains five files ending with the "A" example .txt "file. The system administrator wants to combine them and display the names of the five files at the beginning of the new file. In this case, the following command can be used.
(Ls? X A *. txt; echo; cat A *. txt)> new.txt
In this command, ls is used to list the names of all files to be merged. This result is written to the merged file through the redirection symbol. The echo command in this command is used to insert an empty line between the header and the C language code. That is to say, after executing this command, the program file name will be displayed in the header column, so that the beginning of the code list is like creating a directory.
The author finally stressed that when the standard output redirection function is used, when the output of a command is redirected to a file, the output file is created by shell before the command is executed. If the output file does not exist, shell will automatically create it before the command is executed. If the output file already exists, shell will overwrite the existing file. Therefore, when using this function, it is best to use commands such as ls to check whether the file exists, so as to avoid unnecessary losses due to misoperations.
[1] [2] Next page