II. Standard input / output and redirection 1. Standard input and output We know that three standard files are automatically opened when performing a shell command line, ie standard input file (stdin), usually corresponding to the keyboard of the terminal; standard output file (Stdout) and the Standard Error Output File (STDERR), these two files correspond to the screen of the terminal. The process will get input data from the standard input file, and output the normal output data to the standard output file, and send the error message to the standard error file. In the case of a CAT command, the function of the CAT command is to read the data from the file given by the command line and send them directly to the standard output. If you use the following command: $ Cat Config will display the contents of the file config to the screen. However, if there is no parameters in the command line of the CAT, it will read the data from the standard input and send it to the standard output. For example: $ CATHELLO WorldHello WorldByeBye $ WC << Delim> this text forms the content> of the here document, Which> Continues Until the end of> text delimter> Delim4 17 98 In the << Operator, any character can be used as a separator before the beginning of the text, This example uses DELIM as a separator. The body of the Here document has continued until the other separator is met. The second separator should appear at the beginning of the new line. At this time, the body of the Here document (not including the start and end separator) will reordforward to the command WC as its standard input. Since most commands specify the file name of the input file on the command line in the form of parameters, the input redirection is not often used. Despite this, when a command that does not accept file name as an input parameter is used, the input content is used to solve the problem with an input redirection. 1. Output Redirection Output Redirects Refiguring the standard output or standard error output of the command (or executable program) to the specified file. Thus, the output of the command is not displayed on the screen, but writes to the specified file. Output redirection is more common than input redirects, and this function can be used in many cases. For example, if there is a lot of output, you can't display it on the screen, then redirect to a file, then use the text editor to open this file, you can view the output information; if you want to save the output of a command This method can also be used. Also, the output redirection can be used to use the output of a command as an input to another command (there is a simpler method, the use of pipes, will be described below). The general form of output redirection is: command> file name. For example: $ ls> Directory.Out $ cat directory.outch1.doc ch2.doc ch3.doc chimp config mail / test / $ saving the output of the ls command as a file called Directory.out. Note: If the file behind the symbol already exists, then this file will be rewritten. To avoid the output redirection of the specified file can only store the output redirection of the current command, the shell provides an additional means of output redirection. Output additional redirection is very similar to the output redirection, the difference is only that the function of outputting the additional redirection is to append the output of the command (or executable program) to the final result of the specified file, and the original content of the file is not destroyed. . If you want to add a command to the output of the command to the specified file, you can use additional redirect operators >>. Form is: Command >> File Name. For example: $ ls * .doc >> Directory.Out $ cat directory.outch1.doc ch2.doc ch3.doc chimp config mail / test / ch1.doc ch2.doc ch3.doc $ and the standard output redirection, The error output of the program can also be reordered. Use symbol 2> (or appended symbol 2 >>) to represent the redirection of the error output device. For example, the following command: $ LS / USR / TMP 2> Err.file can see the normal output result of the program on the screen, but send any error information of the program to file err.file to check for future check. You can also use another output redirect operator (&>) to send the standard output and the error output to the same file. For example: $ LS / USR / TMP &> OUTPUT.FILE uses the redirection to combine the command, you can implement new features that the system is unable to provide. For example, using the following command sequence: $ LS / USR / BIN> / TMP / DIR $ WC -W tmp / dir459 counts the number of files in the / usr / bin directory. The pipelines uses the output of a program or command as the input of another program or command, and there are two ways, one is combined with two commands or programs, such as / TMP / DIR file in the previous example Level the LS and WC commands; the other is the pipe function provided by Linux. This method is better than the previous method. The pipe can connect a series of commands, which means that the output of the first command will be transmitted to the second command as the input of the second command, and the output of the second command will be input as the third command. In this class. Displayed on the screen is the output of the last command in the pipe line (if an output redirection is not used in the command line). Establish a pipeline by using pipes "|". Examples of the above: $ ls / usr / bin | wc -w1789, such as: $ cat sample.txt | grep "high" | WC -L pipelines the output of the CAT command (list of a file) Give the grep command. The grep command looks for the word high in the input, the output of the grep command is all lines that contain the word high, and this output is sent to the WC command, the WC command statistics the number of rows in the input. Suppose the contents of the Sample.txt file are as follows: THINGS to do Today: low: Go Grocery ShoppingHigh: Return Movieh: Clear Level 3 in Alien vs. PREDATORMEDIUM: PICK UP CLOTHES from Dry Cleaner The result is 2. The command replacement command replacement and redirects are some similar, but the difference is that the command replacement is the parameter of the output of one command as the other command. The common command format is: Command1 `command2. Among them, the output of Command2 will be used as the parameters of Command1. It should be noted that the `symbol here, which is executed as a command, and the resulting result is the parameters of Command1. For example: $ cd `pwd` This command lists the directory of the PWD command as the parameter of the CD command, and the result is still in the current directory. The 22nd lesson operations in Bash 2000 / May / 29 Command and file name extension feature Bash command line has commands and file name extension features. When entering a command or file name that has not been completed, just type the Tab key to activate the command and file name extension characteristics, thereby completing the remaining input of the command. If multiple commands or files are the same, BASH will ring and wait for the user to enter enough characters so that the unique command or file name, if found, the system will automatically replenish the command or file name, the user presses After the Enter key, the system will execute this instruction. For example: $ CAT pre For example: $ lsdocument docudrama $ cat doc The easiest way is to use the up and down arrow keys to display the previous events to the command line. This operation can be executed without having to run the History command. Press the up arrow key, then an event on the last execution will appear on the command line, then press the previous event, will appear on the command line; press the down arrow key, will make The next incident of the current event appears on the command line. BASH can also complete the character extension of historical events by typing ESC, Tab keys. Like the standard command line extension characteristics, type some strings of historical events, then type ESC, and type the Tab key, and the historical events that match the string that just typed will be automatically extended and returned to the command line. If more than one event matches the input string, you will hear a ringing, continue to type characters or strings, and the shell will only determine the historical event to be typed. There is also a command to query and execute historical events -! Command. Type characters associated with historical events after the command, this associated character can be the historical event number of the historical event, or the first few characters of the event. In the example below, query the event number of the historical event number 3, and then match the characters you start, and query the command. [Example] $! 3mv MyData Reports $! MVMV MyData Reports can also query historical events with an offset (relative to the last event in the list of historical events). The negative offset will be ordered from the historical event list. In the following example, the event "VI MyData" is queried with a negative offset. It must be noted that this offset is the last event relative to the historical event list. In this example, the last event in the list of historical events is event 5, and the first event in the historical event list is 1. From the incident of the historical incident number 5, it is shifted forward 4, which is an event of the historical event number 2. [Example] $! -4vi mydata If you type !!, the system defaults to the previous event. In the following example, the user typed on the command line !! Command, the system will execute the previous event: "LS" command. [Example] $ !! LSMYDATA Today Reports can also search for a historical event with "Mode". Search "Mode" must be used to enclose. The following example uses "Mode" "" MYD? "To search for historical events" VI MyData ". [Example] $!? Myd? Vi mydata1. Query history events can edit the events in the historical event list on the command line. Table 10-3 lists the various operations of the query historical event list. Table 10-3 Query History Event Operation Query History Event Operation Function Ctrl N or down the cursor button to move the next historical event in the list of current events Ctrl P or up the cursor button to the current event in the historical event list Previous history ESC