2. Third. Standard InputOutput and Redirection (Shell Command)

xiaoxiao2021-03-06  58

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 $ user enters each line to be output from the CAT command to the screen. Another example, the command sort reads the file body by line (when the file name is given in the command line, it is sorted from the standard input), and the result is sent to the standard output. The following example is to read a purchase order from the standard input and sort it. $ Sortbanascarrotsapples AppleSbanAascarrots $ At this time we got sorted procurement list on the screen. The following issues exist directly using standard input / output files: When entering data from terminal input, the user can only use the data that is high in half a day. The next time you want to use this data to re-enter. And when entering the terminal, it is not very convenient to modify the input. The information output to the terminal screen can only be seen. We cannot output more processing on this, such as further processing as an input of another command, and so on. In order to solve the above problem, the Linux system is input, and the output is introduced into two other mechanisms, namely the input / output redirection, and the pipe. 2. Enter the redirected input redirection refers to the standard input of the command (or executable program) to the specified file. That is, the input may not be from the keyboard, and from a specified file. Therefore, the input redirection is mainly used to change the input source of a command, especially the input source that requires a large number of inputs. For example, the command WC statistics specifies the number of rows, words, and characters of the file. If you type only on the command line: $ WCWC will wait for the user to tell you what, then the shell is like a dead, all the text typed from the keyboard appears on the screen, but there is no result until it presses < CTRL D>, WC will write command results on the screen. If a file name is given as a parameter of the WC command, as shown in the following example, the WC will return the number of rows, words, and character number included in the file. $ wc / etc / passwd20 23 726 / etc / passwd $ another method to pass the / etc / passwd file content to the WC command is to redirect the input of the WC. The general form of the input redirection is: command

$ 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 $ CAT PrefaceBash can also list partial file names in the current directory to complete file name extension. If you type an ESC, type it?, The shell will list all file names that match the input string. For example, in the next example, type ESC after the input is not completed?, The shell will list all the strings matching the input string, then the shell returns the command line, according to the list of files, you can type the file you want to enter. Name or press the TAB button to complete the file name extension.

For example: $ lsdocument docudrama $ cat doc Documentdocudrama $ cat docudrama [Example] The following is a list of files in a directory included: Firebird2.7.tgz Firebird.README Firebird2.60.tgzFireBird Firebird2.60.tgz.README now Delete the firebird2.60.tgz.readme file, type: $ rm -f fi system will issue an alert, and automatically make the command line to: $ rm -f fire and wait for the user to further enter the back section of the file name . Now type: b system once again issues the sound and automatically makes the command line to: $ RM -F Firebird and wait for the user to further enter the back section of the file name. Now type: 2.6 system once again issues an alert voice, and automatically makes the command line to: $ RM -F Firebird2.60.tgz and wait for the user to further enter the back section of the file name. Now type:. This command will be completed: $ rm -f firebird2.60.tgz..ready can be seen from the previous example, BASH always try to complete the command according to the information entered by the user. When it is impossible to complete the command according to the existing information, the user is prompted to give more information, then the command will be completed according to the user's prompt. As a user, it is best to give sufficient information at a time to make the BASH to make a command completion; otherwise it will take a few times , and the time will be consumed. The command line editing can edit the command line in BASH so that the user can modify the typed command before performing the typed command. If you have a spelling error while typing commands, just use the editing command to correct the editing error before running the typed command, then execute it without re-enter the entire line command. This feature is especially useful for commands with long path file names. Table 10-2 is a summary of the command line editing operation. Table 10-2 Command line Edit Operation command line Editing Operation function Ctrl b or left arrow key left shift a character (moving to the previous character) Ctrl f or right arrow key to move a character (moving to the latter) Ctrl A Move to the head Ctrl E Move to the line ESC B left shift a word ESC F right shift a word DEL Delete the character Ctrl D delete the cursor in the Ctrl H to remove the left side of the cursor Character Ctrl K Delete to the row command history In Bash, the history command saves the most recently executed command. The history number of these commands starts with 1, only limited commands can be saved, up to 500, namely the history number of the history command is 500. To view the recently executed command, just type the History command, then type the Enter key, the recently performed command is displayed in the order of order (the number before the command is history number). [Example] $ History1 CP MyData Today2 VI MyData3 MV MyData Reports4 CD Reports5 LS ... All of these commands are called events, and an event indicates that an operation has occurred, that is, a command has been executed. These events use digital identity based on their success, this identification is called the historical event number. The event number of the last historical event is the largest. Each event can be determined by its historical event number or initial character or string of commands. Use the history command to query the previous event and display them to the command line to execute this event.

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 Move to historical event list tail! Event_num to locate a historical event with historical event numbers! Characters uses historical events with historical events!? Pattern "Mode" to query the events in the historical event list! -Event_num locates historical events through the offset 2. Configure the History: Histfile and HISTSIZE system Save the number of historical events being saved in a specific system variable, this variable is HISTSIZE . The default value of this variable is usually set to 500. This value can be modified. For example: $ HISTSIZE = 10 Reset the value of the HISTSIZE to 10. Historical events are saved in a file, and the file name is specified by the variable histfile. Usually the default name of this file is .bash_history. You can specify a new file name by assigning a value of the Variable Histfile.

转载请注明原文地址:https://www.9cbs.com/read-112945.html

New Post(0)