Linux Basics (7)

And command execution sequence control conduit

The execution order of control command

Under normal circumstances, we can only enter a terminal command, press the Enter execution, execution is completed, then enter the second command, and then press Enter to execute ......, sometimes when we will enter more than once command execution process this time is how to do?

  1. Order to perform multiple commands

Chestnut: When we need to use apt-getto install a software, then install the software (or command tool) run immediately after the completion of the installation, but also happens to be your host only replacement source software has not been updated list of software, then you may have a series of operations as follows :

$ sudo apt-get update
# 等待——————————然后输入下面的命令
$ sudo apt-get install some-tool //这里some-tool是指具体的软件包
# 等待——————————然后输入下面的命令
$ some-tool

Next, using the following order of execution, disposable losers command, and let it go again performed, can be performed using simple sequential ;accomplished:

$ sudo apt-get update;sudo apt-get install some-tool;some-tool
# 让它自己运行
  1. Selective execution command

About the above operation, if we have it automatically execute the command sequence, the previous command execution is not successful, and the latter in turn depends on the results of the command on a command, then it will cause it took time, but eventually get an error As a result, intuitive look and sometimes you can not judge whether the results are correct. So we need to be able to selectively execute commands, such as the previous command executed successfully before continuing to the next, or what should not succeed to make any other treatment, such as we use whichto look up whether to install a command, if found on the implementation of the command, otherwise do nothing:

$ which cowsay>/dev/null && cowsay -f head-in ohch~

The above &&is used to achieve selective executed, which indicates if the previous command execution result (the output terminal of the content is not represented, but rather a state of command execution result) is performed later returns 0, otherwise it does, you can from $?Gets the environment variable returns the results of a command.

$ which cowsay>/dev/null || echo "cowsay has not been install, please run 'sudo apt-get install cowsay' to install"

||Here is the &&control effect contrary, when the result of execution on a command ≠0($?≠0)when the command execution behind it.

In addition to the basic above-described can also be combined with && and || to implement some operations, such as:

$ which cowsay>/dev/null && echo "exist" || echo "not exist"

pipeline

A communication pipe is a mechanism commonly used for inter-process communication (can also be a network communication through Socket), which is manifested in the form of in front of the output of each process (stdout) directly as input to the next process (stdin) .

Pipeline is divided into anonymous pipes and named pipes, we often use when using some filters is anonymous pipes, the command line by |representing the separator, |in front of the content we have used many times to. Named pipes simply means that there is the name of the pipes, named pipes usually only used in the source program.

Chestnuts:
see /etcwhich files and directories under the directory, use the lscommand to view:

$ ls -al /etc

There is too much content, the screen can not be fully displayed, this time you can use the scroll bar or shortcut keys to scroll the window to see. But this time you can use the pipeline:

$ ls -al /etc | less

Pipe the previous command ( lsoutput) as the next command ( less) is input, and line by line can be seen.

  1. cut command to print a field of each line

Print /etc/passwdfile to :as a first delimiter field and sixth fields indicate the user name and its home directory:

$ cut /etc/passwd -d ':' -f 1,6

Print /etc/passwdthe file before each line of N characters:

# 前五个(包含第五个)
$ cut /etc/passwd -c -5
# 前五个之后的(包含第五个)
$ cut /etc/passwd -c 5-
# 第五个
$ cut /etc/passwd -c 5
# 2到5之间的(包含第五个)
$ cut /etc/passwd -c 2-5
  1. grep command to find the matching string in the text or in stdin

grepCommand is very powerful, and it is quite a common command, which combines regular expressions can be very complex to achieve very efficient match and look, introduction here it simple to use.

grepThe general form of the command is:

grep [命令选项]... 用于匹配的表达式 [文件]...

For example: search /home/shiyanlouthe directory containing " shiyanlou" text file, and display line numbers appear in the text:

$ grep -rnI "shiyanlou" ~

-rParameter represents the file recursively search subdirectories, -nmeans print the line numbers match, -Iomit the binary file. This operation does not actually make much sense, but you can feel the grepcommand of powerful and practical.

Of course, you can use regular expressions to match in the field, the following simple demonstration:

# 查看环境变量中以"yanlou"结尾的字符串
$ export | grep ".*yanlou$"

Where $it means the end of a line.

  1. wc command, simple little tool count

wcCommand is used to count the number of output a file and BOC, words and bytes, such as the output /etc/passwdstatistics file:

$ wc /etc/passwd

Output only the number of rows, respectively, the number of bytes of words, bytes, and the number of characters in the longest line of input text:

# 行数
$ wc -l /etc/passwd
# 单词数
$ wc -w /etc/passwd
# 字节数
$ wc -c /etc/passwd
# 字符数
$ wc -m /etc/passwd
# 最长行字节数
$ wc -L /etc/passwd

Note: For Western character, one byte is a character, but a character for Chinese characters is greater than 2 bytes, the number is determined by the specific character encoding.

Again combined pipeline to operate it, all the following statistics the number of directory / etc the following:

$ ls -dl /etc/*/ | wc -l
  1. Sort sort command

sort command input function is to sort by certain way, and then output it supports the sort has sorted dictionaries, digital sorting, sort by month, in random order, reversed ordering, specify the particular field to sort, and so on.

The default is to sort the dictionary:

$ cat /etc/passwd | sort

Reverse the sort:

$ cat /etc/passwd | sort -r

Sort by specific fields:

$ cat /etc/passwd | sort -t':' -k 3

The above -tparameters for the specified field delimiter, is where " :" as the delimiter; -k 字段号for specifying which field to sort. The third field where / etc / passwd file is digital, default sort order is dictionary, if you want to sort according to figures shall add the -n option:

$ cat /etc/passwd | sort -t':' -k 3 -n
  1. uniq to re-order

uniqCommand output may be used for filtration or duplicate rows.

  • Filter duplicate rows

You can use historythe command to view the commands have been executed recently (actually read $ {SHELL} _history file), but might want to see the use of which command did not need to know what specific, you will want to remove the command parameter behind then remove the repeated command:

$ history | cut -c 8- | cut -d ' ' -f 1 | uniq

Then filtered through layers, you will find that indeed only the output of the command of that column, not the past effect of weight seems obvious, look carefully you will find that it does go heavy, but less obvious, reason is not obvious because uniq command only to successive duplicate rows, not the full text go heavy , so to achieve the desired results, we first sort:

$ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq
# 或者$ history | cut -c 8- | cut -d ' ' -f 1 | sort -u
  • Output duplicate rows
# 输出重复过的行(重复的只输出一个)及重复次数
$ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq -dc
# 输出所有重复的行
$ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq -D


source

Published 33 original articles · won praise 1 · views 1232

Guess you like

Origin blog.csdn.net/weixin_44783002/article/details/104906645