Redirect using input/output
In Linux Command Line mode, if the input required by the command is not from the keyboard, but from the specified file, this is the input redirection. Similarly, the command output can be written to a specified file instead of displayed on the screen. This is the output redirection.
Next let's take a look at the practical application of standard input/output redirection in constructing a Linux Command.
1. input redirection and output redirection
# WC aa.txt
# Ls> home.txt
The first command is to input the aa.txt file as the WC command, and calculate the number of lines, words, and characters of aa.txt. The second command saves the lscommand output to a file named home.txt. If the file behind the symbol already exists, the file will be overwritten.
2. Use both input and output redirection
# Iconv-F gb18030-T UTF-8 new-aa.txt
This command uses the same input direction (new-aa.txt ). Aa.txt is a gb18030 file, while new-aa.txtis a new file that converts the content of aa.txt into UTF-8 format.
Pipeline
The pipeline operator "|" provided by Linux is used to separate the two commands. The output of the command on the left of the pipeline operator is used as the input of the command on the right of the pipeline operator. The continuous use of pipelines means that the output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the Third Command, and so on. Next let's take a look at how the pipeline gets an application in constructing a Linux Command.
1. Use a pipe
# Rpm-Qa | grep licq
This command creates an MPS queue with the pipe character "|. The MPs queue uses rpm-Qa command output (including all installed RPM packages in the system) as the grep command input to list RPM packages with licq characters.
4. Use multiple pipelines
# Cat/etc/passwd | grep/bin/bash | WC-l
This command uses two pipelines and sends the cat command (display the content of the passwd file) to the grep command using the first pipeline, the grep Command finds all rows containing "/bin/bash". The second pipe sends the grep output to the WC command, and the WC command counts the number of rows in the input. The function of this command is to find out how many users in the system use Bash.
Use commands to replace
In Linux Command Line Mode, when a pair of "'" (top delimiter) is encountered, the commands contained in "'" will be executed first, then, the output result is substituted into the command line as a parameter, which is replaced by the command. It is similar to the redirection function of input and output, but the difference is that the command is replaced with the output of one command as the parameter of another command. Next we will look at its practical application.
1. Use commands to replace
# Touch 'date when policyymm1_d1_k1_m1_s'.txt
This command is replaced with a command. The date + % Y % m % d % K % m % s command will be executed first, and it will output the current time in the specified format. Then, the time will be used as a parameter of the touch command. The result is that a file with the current time as the file name is created.
2. Use commands to replace
# Kill '/sbin/pidof smbd'
This command will kill all sshd processes. Here, the pidof command is used to give the process number, because kill operates on the process number. Both of them are replaced by commands, implementing the function of killing all sshd processes with only one command.
Comprehensive Application
After understanding and familiarizing yourself with the previous skills, it is a high skill to apply them comprehensively. At the same time, some commonly used and complex Linux commands must be mastered. Some basic and important commands frequently used in constructing Linux Commands include grep, TR, sed, awk, find, cat, and echo. Here are some examples of comprehensive applications.
1.
# Man ls | col-B> ls.man.txt
This command uses both output redirection and pipelines to convert ls help information into a text file that can be directly read.
2.
# Kill-9 'ps-Ef | grep smbd | tr-S' | awk-f'' {print $2 }''
The functions in this example are the same as those above, but it does not rely on pidof commands. It uses commands in most Unix systems and is suitable for more system environments. But it looks more complicated. It uses three pipelines and one command replacement, and grep, TR, and awk commands related to character operations.
From the previous introduction, we can see that through the combination of several techniques, Linux commands can complete complex functions. In addition, you can organize these commands into a script, add functions such as functions, variables, judgments, and loops, and add some programming ideas, which is a shell script with more powerful functions.