Introduced
Sequential execution, select execution, piping, cut commands, grep commands, WC commands, sort commands, and more, efficiently use Linux techniques.
I. Control of order execution sequence 1. Sequential execution of MULTIPLE commands
Usually, we can only enter a command at the terminal at a time, press ENTER to execute, after the completion of the execution, we then enter the second command, and then press ENTER to execute ... You may encounter the following usage scenarios: I need to install a software and apt-get
then run the installed software (or command tool) immediately after installation, and it happens that your host has not replaced the software source has not updated the software list (for example, in our environment, each time we restart the experiment will have sudo apt-get Update, which does not already have this problem), then you may have the following series of actions:
$ sudo apt-get update# 等待——————————然后输入下面的命令$ sudo apt-get install some-tool# 等待——————————然后输入下面的命令$ some-tool
At this point you might think that if I can enter it all at once, let it run the commands on its own, and that's the problem we're going to address in this section.
Simple sequential execution You can use ;
to complete, such as the above operations you can:
update;sudo apt-get install some-tool;some-tool# 让它自己运行
2. Selective execution of commands
About the above operation, I do not know whether you have thought of a problem, if we let it automatically sequential execution of the command, the previous command execution is unsuccessful, and the subsequent command depends on the result of the previous command, then it will take time, and finally get a wrong result, And sometimes intuitively you can't tell if the results are correct. Then we need to be able to execute the command selectively, such as the previous command to continue the success of the next, or unsuccessful and what else to do, such as we use which
to find out whether to install a command, if found to execute the command, otherwise do nothing (although this operation is not meaningful, But can help you understand some of the concepts better):
$ which cowsay>/dev/null && cowsay -f head-in ohch~
If you do not have cowsay
the installation, you can first execute the above command, you will find that nothing happened, you install again after you run the above command, you will find some surprises.
The above &&
is for selective execution, which means that if the previous command execution results (not the contents of the terminal output, but the result of the command execution state) return 0 to perform the following, otherwise do not execute, you can get the result of the $?
last command from the environment variable:
Users who have learned the C language should know that in C the &&
table is logical and that there is a ||
representation logic or that the same shell has one ||
, and that the difference is that the two symbols in the shell can also be used to represent logic and and or apart, is simple control of the order in which commands are executed here. ||
here is the &&
opposite control effect, when the previous command executes the result of ≠0 ($?≠0), then executes the command following it:
which cowsay>/dev/null || echo "cowsay has not been install, please run ‘sudo apt-get install cowsay‘ to install"
In addition to the above basic use, we can also combine this &&
and ||
to achieve some operations, such as:
which cowsay>/dev/null && echo "exist" || echo "not exist"
I'll draw a flowchart to explain the process above:
Thinking
In the above we &&
will ||
use and combine, then whether in any order of the line? For example, above we are &&
in the front ||
, in turn can we? Will there be a problem?
Second, the pipeline
What a pipeline is, a pipeline is a communication mechanism that is typically used for inter-process communication (or network communication via a socket), which is represented by the output (stdout) of each of the preceding processes as input to the next process directly (stdin).
Pipelines are also classified as anonymous and named pipes (this will not discuss the use of system calls to create and use pipelines in the source program, which is actually the same mechanism as the command line's pipeline in the kernel). When we use some filter programs, we often use anonymous pipes, which are represented by delimiters in the command line |
, which |
we have used many times in the previous content. A named pipe is simply a named pipe, usually a named pipe in the source program. Below we will use some common "filter programs" that can be used to help you get used to the plumbing.
1. Trial
Try the pipeline first, such as to see /etc
what files and directories are in the directory, and use ls
commands to view:
$ ls -al /etc
There is too much content, the screen cannot be fully displayed, and you can use the scroll bar or shortcut keys to scroll through the window. However, you can use pipelines at this time:
$ ls -al /etc | less
The output of the previous command () is entered by the pipeline ls
as the next command ( less
), and then it can be viewed in one line.
2.cut command to print a field for each line
In the print file, the /etc/passwd
:
user name and its home directory are represented in the 1th and 6th fields of the delimiter:
-d ‘:‘ -f 1,6
Print /etc/passwd
the first n characters of each line in the file:
# 前五个(包含第五个)$ cut /etc/passwd -c -5# 前五个之后的(包含第五个)$ cut /etc/passwd -c 5-# 第五个$ cut /etc/passwd -c 5# 2到5之间的(包含第五个)$ cut /etc/passwd -c 2-5
3.grep command to find matching strings in text or stdin
grep
Commands are powerful and fairly common commands that combine regular expressions to achieve very complex but efficient matching and lookups, but before you learn regular expressions, it's simple to use, and there will be a separate section about regular expressions that will continue to be learned grep
later. Commands and some other commands.
grep
The general form of the command is:
grep [命令选项]... 用于匹配的表达式 [文件]...
Let's try it first, we search all the /home/shiyanlou
text files in the directory that contain "Shiyanlou", and display the line numbers in the text now:
grep -rnI "shiyanlou" ~
-r
The parameter represents a recursive search for a file in a subdirectory that -n
represents a print match line number, indicating that the -I
binary file is ignored. This operation does not really make much sense, but can feel the grep
powerful and practical command.
Of course, you can also use regular expressions in match fields, and here's a simple demo:
# 查看环境变量中以"yanlou"结尾的字符串$ export | grep ".*yanlou$"
Which $
represents the end of a line.
4. WC command, simple and compact counting tool
The WC command is used to count and output the number of rows, words, and bytes in a file, such as /etc/passwd
the statistics of the output file:
$ wc /etc/passwd
Output only the number of rows, words, bytes, characters, and bytes of the longest line in the input text, respectively:
# 行数$ wc -l /etc/passwd# 单词数$ wc -w /etc/passwd# 字节数$ wc -c /etc/passwd# 字符数$ wc -m /etc/passwd# 最长行字节数$ wc -L /etc/passwd
Note: For Western characters, a character is a byte, but for Chinese characters a character is greater than 2 bytes, the exact number is determined by the character encoding.
Again to combine the pipeline to operate, the following statistics/etc below all directory number:
-l
5.sort Sort Commands
This command before we have been used many times, the function is very simple is to sort the input in a certain way, and then output, it supports sorting by dictionary, number sorting, sorting by month, random sort, reverse sort, specify specific fields to sort and so on.
The default is dictionary ordering:
sort
Reverse Sort:
sort -r
Sort by a specific field:
sort -t‘:‘ -k 3
The above -t
parameter is used to specify the delimiter for the field, where ":" is used as the delimiter, -k 字段号
which specifies which field to sort. /etc/passwd
The third field of the file here is a number, which by default is a dictionary order, and if you want to sort by numbers, add -n
parameters:
sort -t‘:‘ -k 3 -n
6. Uniq to re-order
uniq
Commands can be used to filter or output duplicate rows.
We can use history
commands to view recently executed commands (actually reading the ${shell}_history file, such as the ~/.zsh_history file in our environment), but you may want to see only the command that was used without needing to know exactly what to do, Then you might want to get rid of the arguments behind the command and then remove the duplicate commands:
history | cut -c 8- | cut -d ‘ ‘ -f 1 | uniq
Then through the layers of filtering, you will find that only output the execution of the command that column, not past the heavy effect seems not obvious, look closely you will find it trend to heavy, but not so obvious, the reason is not obvious because the uniq
command can only go to repeat the row, not the full text to weight , So to achieve the desired effect, we first sort:
history | cut -c 8- | cut -d ‘ ‘ -f 1 | sort | uniq# 或者$ history | cut -c 8- | cut -d ‘ ‘ -f 1 | sort -u
This is the place where Linux/unix philosophy attracts people, and it is a command to do only one thing but to the best.
# 输出重复过的行(重复的只输出一个)及重复次数$ history | cut -c 8- | cut -d ‘ ‘ -f 1 | sort | uniq -dc# 输出所有重复的行$ history | cut -c 8- | cut -d ‘ ‘ -f 1 | sort | uniq -D
There are many more text-processing commands, and the next section will continue with some commonly used text-processing commands.
Homework
Use the previously described method, install aview
and imagemagick
, then asciiview
display the picture with a command, using the method can be viewed with the man command.
Getting Started with Linux basics----command execution sequence control and piping