Linux Learning-Bird Brother-Chapter 10-Bash Learning

bashLong-lived wildcards in the operating environment

symbol significance
* Represents [0 to infinity] any character
? Represents [must have one] any character
[] It means there must be a character in brackets
[-] When this character have a minus sign, on behalf of [any character in the coding sequence], e.g. [0-9] represents 0the 9all numbers between
[^] If the first character in the brackets is an exponent symbol [^], the selection is reversed during the table. For example, [^abc]representative, there must be a character, as long as a,b,cthe other characters

bashSpecial symbols in the environment

symbol content
# Comment symbol
\ Escaped characters: restore [special characters or wildcards] to normal characters
| Pipe: Separate two pipe commands
; Continuous command execution separator: the definition of continuous commands (not the same as pipeline commands)
~ User's home directory
$ Use variable leader to add variable substitution value before variable
& Job management (job control): Turn commands into background tasks
! [non]
/ Directory symbol: path separator
>、>> Data stream redirection: output orientation, respectively replacement and accumulation
‘’ Single quotes, without the function of variable substitution
“” Double quotes, with the function of variable substitution
( ) In the middle is the start and end of the subshell
{ } In the middle is a combination of command blocks

1. Data flow redirection

Data stream redirection (redirect) refers to the data that should appear on the screen after a command is executed and saves it to other places. For example, files or devices (printers, etc.)

1. What is data stream redirection

After executing the command, standard output (STDOUT) and standard error output (STDERR) will be generated on the screen by default.

  • Standard output (STDOUT)
    refers to the correct information returned by the command execution
  • Standard error output (STDERR)
    Standard error output can be understood as after the command execution fails, the returned error information
    data stream can redirect standard output (stdout) and standard error output (stderr) to other files or devices, respectively. , The special characters used for transmission are as follows:
  • Standard input (stdin): code is 0, use <or<<
  • Standard output (stdout): code is 1, use >or>>
  • Standard error output (stderr): code is 2, use 2>or2>>

2. When to use data stream redirection?

  • The information on the screen is important, and when we need to save it
  • Program running in the background, do not want it to interfere with the normal output of the screen
  • Some execution commands may [2>null]be thrown away when the error message is known
  • When error information and correct information need to be output separately

3. Judgment basis for command execution:;, &&, ||

  • cmd;cmd(Continuous command execution regardless of command correlation)
  • $?(命令返回值)With &&or||
Command execution Explanation
cmd1&&cmd2 If the cmd1execution is completed and executed correctly ($? = 0), the execution will start cmd2, otherwise it will cmd2not be executed
cmd1||cmd2 If the cmd1execution is completed and correct and wrong ($? ≠ 0), the execution beginscmd2

Second, the pipeline command (pipe)

The pipeline command pipe is not the same as the continuous command. You need to pay attention to the following two points:

  • Pipeline commands only process standard output and ignore standard errors
  • Pipeline commands must be able to accept data from the previous command as standard input to continue processing

1. Select the command: cut , grep

Here are two commonly used information selection commands

  • cut
  cut -d'分割字符' -f fields <==用于有特定分隔字符
  cut -c 字符区间 			 <==用于排列整齐的信息
#选项与参数
  `-d`:后面接分割字符,与`f`一起使用
  `-f`:根据`-d`的分割字符将一段信息划分为数段,用`-f`取出第几段
  `-c`:以字符的单位取出固定字符区间

Example 1:
[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-xZ1fHR1p-1586780751510) (C: \ Users \ lee \ AppData \ Roaming \ Typora \ typora-user -images \ 1586761690172.png)]
Example 2 (only data after the 12th character is displayed):

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-iaQ8fUye-1586780751512) (C: \ Users \ lee \ AppData \ Roaming \ Typora \ typora-user-images \ 1586761804497.png)]
cutThe main purpose is to decompose the data in the same row, but it will be more difficult when processing data connected by multiple spaces.

  • grep
    grepis to analyze a line of information, if there is information needed, take the line out
  `grep` [-acinv] [--color=auto] `查找字符` filename
#选项与参数
  `-a`:将二进制文件以文本文件的方式查找数据
  `-c`:计算找到`查找字符`的次数
  `-i`:忽略大小写
  `-n`:顺便输出行号
  `-v`:反向选择,显示出没有`查找字符`内容的那一行

Examples:

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-DQmwe2tU-1586780751513) (C: \ Users \ lee \ AppData \ Roaming \ Typora \ typora-user-images \ 1586770518712.png)]

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-uLsXvJl7-1586780751514) (C: \ Users \ lee \ AppData \ Roaming \ Typora \ typora-user-images \ 1586770638578.png)]

2. Sorting commands: sort , wc , uniq

  • sort
  sort [-fbMnrtuk] [file or stdin]
#选项与参数
`-f`:忽略大小写差异
`-b`:忽略最前面的空格字符部分
`-M`:以月份的名字来排序
`-n`:使用[纯数字]进行排序(默认是以文字的形式进行排序)
`-r`:反向排序
`-u`:uniq,相同的数据中,仅出现一行代表
`-t`:分隔符号,默认用[tab]来分割
`-k`:以哪个区间(field)进行排序。
  • uniq
#可以用来对排序好的数据进行去重处理
uniq [-ic]
`-i`:忽略大小写字符的不同
`-c`:进行计数
  • wc
    wccan be used to calculate the overall data of the output information
  wc [-lwm]
#选项与参数
`-l`: 仅列出行
`-w`:仅列出多少字(英文字母)
`-m`:多少字符

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-iQPHYVHD-1586780751515) (C: \ Users \ lee \ AppData \ Roaming \ Typora \ typora-user-images \ 1586775173306.png)]

3. Double redirection: tee

teeData stream can be sent to file and screen, and the function of output to screen is stdoutsimilar

  tee [-a] file
#选项与参数
-a: 以累加的方式,将数据加入到file中
last | tee last.list | cut -d " " -f1

4. Character conversion commands: tr, col, join, paste, expand

As mentioned earlier, the DOSnewline UNIXcharacter is different from the newline character. You can use dos2unixand unix2dosto complete the conversion. The following describes the commands that are replaced by other commonly used characters.

  • tr is
    trused to delete the text in the message, or to replace the text message
  tr [-ds] SET1
`-d`:删除信息当中的 SET1字符
`-s`:替换掉重复的字符
  • col
#选项与参数
`-x`:将tab键转换成对等的空格键
  • join
    joinis used to process data between two files,
  join [-ti12] file1 file2
#选项与参数
`-t`:join默认以空格字符分割数据,并且比对[第一个栏位]的数据,若相同,则将两条数据连城一行
`-i`:忽略大小写差异
`-1`:代表[第一个文件要用哪个栏位来分析]
`-2`:代表[第二个文件要用哪个栏位来分析]
  • paste
  paste [-d] file1 file2
#选项与参数
`-d`:后面可以分割字符,默认以[tab]来分割
`-`:file部分可以写成-,表示来自标准输入
  • expand
#将[tab]键转成空格
  • unexpand convert spaces to [tab]

5. Divide command: split

  split [-bl] file PREF
#选项与参数
`-b`:后面接想要划分成的文件大小,可以加单位
`-l`:以行数来进行划分

6. Parameter substitution: xargs

xargs

xargs
#这个参数不明白

7. Use of minus sign

When some commands need to use a file name to be processed, stdinand stdoutcan -be replaced, for example

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-WVxvrQ0i-1586780751517) (C: \ Users \ lee \ AppData \ Roaming \ Typora \ typora-user-images \ 1586779304393.png)]

3. Key review

  • shellReasons for learning : LinuxMany of the command management systems are shellwritten in
  • bashThe main functions: history command, command and file completion function, command alias setting function, task management, foreground and background control, programmatic script, wildcard
  • envWith exportobservable environment variables, exportyou can convert custom variables into environment variables
  • setCan observe bashall variables in the current environment
  • $?, Representing the return value of the previous command, and Linuxreturning 0 represents successful execution
  • The wildcards are: *,?, [], Etc.
  • Data flow redirection uses the symbols such as>, 2>, <to transfer the output information to other devices
  • Pipe command cutare: grep, sort, wc, uniq, tee, tr, , splitetc.
  • The main point of the pipeline command is that the pipeline command will only process standard output and ignore standard errors.

variable

  • $?, Representing the return value of the previous command, and Linuxreturning 0 represents successful execution
  • The wildcards are: *,?, [], Etc.
  • Data flow redirection uses the symbols such as>, 2>, <to transfer the output information to other devices
  • Pipe command cutare: grep, sort, wc, uniq, tee, tr, , splitetc.
  • The main point of the pipeline command is that the pipeline command will only process standard output and ignore standard errors.
Published 33 original articles · praised 4 · 30,000+ views

Guess you like

Origin blog.csdn.net/leaeason/article/details/105496665