⭐ Personal notes of 100 lectures on Linux practical skills – 4. Shell

Time:2021-1-25

[TOC]

Get to know shell

What is shell

Shell is a command interpreter, which interprets commands to kernel for execution

user
👇
Shell
👇
kernel

View all shell types

cat /etc/shells
/bin/sh
/Bin / bash # BSH based enhanced refactoring is the default shell of CentOS 7 and Ubuntu
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh

Linux startup process

Start sequence from top to bottom

        BIOS basic input and output system
        |
        The main boot part of MBR hard disk (the first 446 bytes are the main boot record, and the first 512 bytes include the partition table in addition to the main boot record)
        |
        Bootloader (grub) is a tool to start and boot the kernel. At present, it uses / boot / Grub2
        |
        Kernel kernel
CentOS 7 |   CentOS 6   
        /  \       
SYSTEMd init process 1 (in CentOS 6, it is / usr / SBIN / init process, and in CentOS 7, it is / usr / lib / SYSTEMd / SYSTEMd)
      |         |
The system initialization is completed by shell script. Part of CentOS 7 is configured by SYSTEMd and booted by application program. The system initialization is still completed by shell script
      |
   shell

The boot of CentOS 6 after init is slightly different from that of CentOS 7

In CentOS 6, init boot steps

  1. /etc/rc.d/rc.sysinitSystem initialization
  2. Waiting for user terminal access

In CentOS 7, the steps of SYSTEMd

  1. /etc/systemd/systemRead boot level
  2. /usr/lib/systemd/systemRead each service
#Export master boot record
dd if=/dev/sda of=mbr.bin bs=446 count=1
hexdump -C  mbr.bin                             #View master boot record

#Export master boot record和磁盘所有分区表
dd if=/dev/sda of=mbr2.bin bs=512 count=1
Hexdump - C mbr2. Bin # the last face ends with 55AA to indicate bootable

How to write shell script

Elements to be included in a standard shell script

  • Sha-Bang

    First line#!The beginning part

    Add first line of text file#!/bin/bashYou can use the. / script.shWhen the script is executed in this way, the currentbashScript, the system will choose the corresponding shell to executeBash script.shWill be treated as a comment

  • #The beginning is regarded as a comment
  • Script execution permission, if binary executable file, only needxFor text files, you need torx
  • It is usually agreed that the bash script has an extension of.sh
  • Can be used in one line;If multiple commands are separated, the commands will be executed one by one in order, and the later command will be executed only after the previous command is executed

Make sure to exit as soon as there is a script execution error

You can set at the beginning of the script:set -eTo tell bash that if any statement fails to execute, exit the script directly to prevent the error from snowballing

here$?unavailable

If inset -eMode, in order to ensure that some statements fail and do not exit the script, the following methods can be adopted

#Method 1
command || { echo "command failed"; exit 1; }

#Method 2
if ! command; then
    echo "command failed"
    exit 1
fi

aboutsetFor more information, seeSet command

Shell script execution mode

Four ways to execute commands

  • bash file.sh

    A bash subprocess will be generated under the current terminal, and then the subprocess will execute the script

    In this way, you don’t need to give the script execution permission

  • ./file.sh

    Similarly, a sub process will be generated under the current terminal according to the Sha bang of the script (that is, the first line of the script)#!/path/to/bash)To explain the script

    For example, the first line of a python script is#!/usr/bin/python

    needGive script execution permission

  • source file.sh

    When the script is executed in the current process, the actions in the script will affect the current environment

    This method also does not need to give the script execution permission

    Because it is executed in the current process, the operation will affect the current processcd /tmpIt will also change the working directory of the current environment

  • . file.sh

    .yessourceIs equivalent tosource file.sh

supplement

  • exec <command>

    usecommandProcess replaces the current process, PID unchanged, and exits directly after command execution

Differences between built-in and external commands

Built in command

  • There is no need to create a child process
  • Takes effect on the current shell

such ascdIs a built-in command, which will switch to the working directory of the current shell

Shell options

Shopt command

View / set shell options

Shopt [option] [option name >]

Examples
    Shopt # view all options and their values
    Shopt < option name >? View the value of the specified option

option
    -S set, set to on
    -U unset, set the value to off
    
部分重要option
    login_ Shell # indicates whether the current is a login shell or a non login shell

Practical examples

Gets the directory of the current script

#Save the current directory to the variable PWD (note the command)
PWD="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd -P)"

BASH_SOURCEA variable is an array whose first parameter is the name of the current script

If usedsourceTo execute the script$0The value of is the name of the parent script, not the name of the current script

Print message with date and time

log() {
  echo $(date "+%Y-%m-%d %H:%M:%S")" "$*
}

log "lala"

Pipeline and redirection

By default, a process will open three file descriptors: standard input, standard output and standard error

  • Standard input is input by terminal by default
  • Standard output and standard error are output to terminal by default

Pipeline and pipeline symbol|

Pipeline and signal are one of the ways of process communication

Anonymous pipeline|)It’s shell that becomes a communication tool

Pipe symbol|To pass the execution result of the previous command to the following command

  • A pipeline is actually a connection between the standard output and the standard input of different processes
  • Change the value of the previous command tostandard output To connect to the next commandStandard input
  • Pipeline operators are run by creating child processes

    If a child process is a shell, it is called a child shell

    Running the built-in command in the command with pipeline character is actually executed in the new sub shell, which will not affect the current shell environment

    Therefore, the use of built-in commands in pipe symbols is generally avoided

  • If the external command is connected, multiple subprocesses will be established in order to execute the external command respectively, and each standard output and standard input will be connected in order

Examples

ps | cat
echo 123 | ps

Examples

⭐ Personal notes of 100 lectures on Linux practical skills - 4. Shell

catThe standard output of the subprocess (1) is redirected to the anonymous pipeline (463978), while thelessThe standard input (0) of is redirected to the same anonymous pipe (463978), that iscatThe standard output of is redirected to thelessStandard input for

Pay attention to the difference between pipe symbol and semicolon

  • There is no relationship between multiple commands separated by semicolons, and only one command will be executed each time
  • Multiple commands separated by pipeline characters have input / output redirection relationship, and will be started almost at the same time (the actual order is from right to right)The executed commands (including built-in commands) are executed in the new subprocess, not in the current shell

Return code

It can be used normally$?Get the execution result status code of the last command (0 normal, not 0 abnormal)

But if it is used after executing a pipeline$?Only the status code returned by the last instruction of the pipeline is obtained

$PIPESTATUSThe variables are similar$?But it holds the return code of each command in the pipeline

  • ${PIPESTATUS[0]}Represents the return code of the first command in the pipeline
  • If the previous command is not a pipe, it will also be updated`$PIPESTATUSvalue

reference resources:https://www.cnblogs.com/suane…

Redirection symbol

Redirection symbols actually connect the standard input and output of a process to a file

  • Using file instead of terminal input
  • Using file instead of terminal output

All redirection symbols include

  • input redirection

    • <
    • <<EOF
    • <<"EOF"Do not escape special characters
    • <<'EOF' Do not escape special characters
  • Output redirection

    grammar
        [< file descriptor = 1 >] < redirection symbol >
    
    Parameter interpretation
        < file descriptor >
            1 standard output (default if not written)
            2 standard error
            &Standard output and standard output
            
        < redirection symbol >
            >Clear and write
            >>Append write
            
    Examples
        &>>Redirecting standard output and standard error to file and appending write
        1> 1. TXT 2 > > 2. TXT # redirect the standard output to 1. TXT file, clear the file and write it. At the same time, redirect the standard error to 2. TXT file and add it
        #Combine input and output redirection (escape content, where variables are replaced, and ` whoamI 'command is executed and replaced)
        cat > /path/to/file <<EOF
        i am $USER
        `whoami`
        EOF
        #Combining input and output redirection (without escaping content)
        cat >> /var/spool/cron/root <<'EOF'
  EOF
###Use input reorientation instead of standard input

input redirection

Read < variable name > < EOF
123
EOF

Redirection from file input

echo 123 > tmp.txt
Read < variable name > < 0/ tmp.txt

>The pipeline cannot be used because the commands in the pipeline are executed in a new process and the read variables cannot affect the current environment
>
> ```sh
>Invalid
>Echo "123" | read < variable name >
> ```
>
> 



##Xargs command

Build and execute the command line from standard input

-It is applicable to the case that the command to be executed can only read the value from the parameter instead of the standard input

Xargs [options] < command = echo >

option

Separator
-d                        # 定义输入Separator, 默认是空白和换行
--null, -0                # 以 null 作为Separator, 常搭配使用(文件名可能有空格, 反斜杠等) `find -print0 | xargs -0`

Split into multiple commands and execute them separately
-N < n >? Use up to < n > parameters to build a command line
-L < n > # - if the standard input contains multiple lines, the - L parameter specifies how many lines to use as a command line parameter (execute multiple commands separately)

-I < replacer > # use - I to specify a replacement string, which will be replaced when xargs is extended. When - I is combined with xargs, each parameter command will be executed once

--Interactive, - P # - one by one command to confirm whether to execute, only reply to y or Y beginning will execute, otherwise skip
--Verb, - t # displays the command to be executed in standard error output before execution

--Max procs < n > # how many processes can run at most at the same time? The default value is 1. If it is 0, it means no limit. It can cooperate with - n to avoid executing exec only once

usage

< previous command > | xargs # rebuild the command to be executed through the pipe character
Xargs # is manually entered by the user (end input with Ctrl + D), and the command to be executed is constructed

Example: echo, RM, MKDIR, LS and other commands

#Simple echo example
echo 123 | xargs echo
#Use every 2 parameters to execute the command
echo {0..9} | xargs -n 2 echo
#Echo was performed 3 times (the following equivalent)
echo -e "a\nb\nc" | xargs -L 1 echo
echo -e "a\nb\nc" | xargs -n 1 echo
echo "a b c" | xargs -n 1 echo
#After finding all the txt files, search each file once to see if it contains the string ABC.
find . -name "*.txt" -print0 | xargs -0 grep "abc"
#Variable

##Variable定义

Shell variables are not type sensitive



Naming rules

-Letters, numbers, underscores
-Don't start with a number



##Variable赋值

###Read command

Interaction mode

Read variable value through standard input

Read variable name

option

-a             # array  assign the words read to sequential indices of the array
            # variable ARRAY, starting at zero
-P < prompt >? Before reading variables, print a prompt text (without wrapping)
-Do not parse the backslash, that is, read in the original value (recommended for normal use)
>When writing shell scripts, you should avoid copying variables interactively unless necessary





*Continuous reading of data in the pipeline*

seq 1 10 | while read line; do echo $line; done;

###Non interactive mode

Note: '='adjacent left and right sides are not allowed to appear spaces



 **String assignment**

Variable name = variable value

eg.

a=123

>Spaces are not allowed around the equal sign
>
>When the variable value contains spaces, it needs to be enclosed with '' "'or ''"
>
>The difference between single quotation mark and double quotation mark:
>
>- single quotation mark: reference commands, reference variables and escape characters in variable values will not be parsed.
>- double quotation marks: parses the reference command, reference variable and escape character in the variable, and then assigns the parsed value to the variable name



**Mathematical expression assignment**

Let variable name = variable value

Examples

Let a = 10 + 20 # A is 30
>Variable values can only be mathematical expressions
>
>Due to the poor computing performance of shell, it is generally not used for computing





**Assigning a command to a variable**

Variable name = command

If the value of a variable is an executable command, you can directly use $< variable name > to execute the command
However, if you want to assign the running result to another variable, you need to cooperate with $or`

Examples

Directly execute variable values
In this case, LS - HL is executed

Assign the value of a variable to another variable
CMD = "uptime"; result = $($CMD) # assign the result of uptime to the result variable
CMD = $uptime; result = '$CMD'; same as above
>It is often used after splicing command



**Assigning command results to variables**

Variable name = $(command)
Variable name=command

Examples

current=`pwd`
current=$(pwd)
##Variable reference

-The '${variable name}' denotes a reference to a variable

-'echo ${variable name}' view the value of a variable

-The '${variable name}' can be omitted as the '$variable name in some cases`

  >Partial emptying refers to: when the variable name is followed by other characters
  
-A reference to a variable by '${! Variable name}'

t1=t2
t2=”i am t2″
Echo $T1 # output “T2”
Echo ${! T1} output “I am T2”

##Scope of action of variables

The scope of a variable is only in the current shell by default, and its parent, child and parallel shell are invisible



>If you want variables defined in a script to take effect in the current shell, there are two ways:
>
>- execute 'source script. Sh' or '. Script. Sh' in the current shell`
> - 



**Variable export**

Execute export in the parent process, so that the child process can get the variables in the parent process

Export variable name [= variable value]

>The variable name modification of parent process in child process is not aware (invalid) in parent process, but it is effective in child process



**Delete variable unset**

Delete variable

Unset variable name

##System environment variables

###Setting environment variables temporarily

Environment variables refer to the variables that can be obtained by opening each shell

>Environment variables are exported, so modifying them will affect the child process



If you just want to temporarily modify an environment variable to execute a program, you can easily do the following:

< environment variable name > = < environment variable value > < command >

Examples

Lang = C man iptables # view the English version of man help
###Explanation of some variables

####Environmental variables

**It's important**

Path # the search path of the current command, separated by: you can add a new command search path in the following ways

        # PATH=$PATH:/path/to/bin
        

PS1 # current terminal prompt text

**Just know**

User # current user name
Uid # current user ID

####Predefined variables

$? # whether the last command is executed correctly, 0 means correct, 1 means error

$$# current process number

$0 # the process name of the process, not the script name!! this concept is different

#If the script is executed in Bash mode, the value is the script name. If the script is executed in source mode, the value is the process name of the parent process
        #It's easy to understand the script execution before this connection

The current line number of the $lineno # shell script

>'$?' is often used to determine whether the last command is executed correctly, so as to realize script automatic exception handling



####Position variable

$* # all parameters of script execution
All parameters of [email protected] # script execution
Number of $# parameters

$1 # first parameter

$9
${10} # the 10th parameter, the brace cannot be omitted at this time

`The difference between $* 'and' [email protected] 'is that they behave differently when enclosed in double quotation marks

When the input parameter is a B C

“$*” # “a b c”
[email protected]” # ‘a’ ‘b’ ‘c

###Env command

View all current variables (including environment variables)

env

###Set command

Modify the running parameters of shell environment

Set # displays all environment variables and shell environment parameters
Set [parameter] [- O option name] [Arg…]? Set shell environment parameters

option

-U # when a non-existent variable is used, an unbound variable is reported and the script is terminated (ignored by default), which is equivalent to - O nounset
-X # open echo. When each command is executed, it will output the executed command, which is convenient for debugging complex scripts (not opened by default). It is equivalent to - O extract

-When the e # command fails to run, exit the script to prevent errors from accumulating. It is recommended to open it in actual development (ignored by default), which is equivalent to - O extract
                #Note that this does not apply to pipes (except in the last subcommand of the pipe)
+E # for temporary closure '- e`

-O pipefail # any subcommand in the pipeline will exit the script if it fails (not by default, even if - E is turned on)

Common writing

set -euxo pipefail
set -eux -o pipefail

You can also pass in from the command line when executing the bash script:

bash -euxo pipefail script.sh
`Some references: http://www.ruanyifeng.com/blog/2017/11/bash-set.html



#### set -e

After 'set - E' is enabled, if some statements are allowed to fail (or other logic needs to be executed after failure), the following writing method can be used

Writing method 1

command || true
command || { echo “fail”; }

Writing method 2

if !command; then

:

fi

Writing 3

Set + e # temporary cancel – e

do something

Set – e # restore – e

###Environment variable configuration file

configuration file

- `/etc/profile`
- `/etc/profile.d/*`
- `~/.bash_profile`
- `~/.bashrc`
- `/etc/bashrc`





####Divided by storage location:

-The configuration under '/ etc /' is common to all users

-The configuration under '~ /' is only valid for individuals





####According to file types:

- `profile`

  Configure environment variables

- `bashrc`

  Alias and function definition





####According to the login and no longin shell

There are two kinds of shells when users log in 



You can use the following command to see which one you currently belong to

shopt login_ Shell # on means login shell and off means non login shell

**login shell**

-Including: ` Su -`

-Will load 'profile' and 'bashrc' class files

-The loading sequence is as follows

su – root

loading /etc/profile
loaded /etc/profile

loading ~/.bash_profile
loading ~/.bash_rc
loading /etc/bashrc
loaded /etc/bashrc
loaded ~/.bash_rc
loaded ~/.bash_profile

graph TB

1 (/ etc / profile) -- step 1
2 (/ root /. Bash profile) -- step 2--3
3 (/ root /. Bash RC) -- step 3--4
4 (/ etc / bashrc) -- step 4--3
3 -- step 5 2
>In the figure above
>
>Since '~' will be escaped, the specific '/ root /' is used instead
>
>Due to`_ `It can't be displayed, so use '-' instead





**no-login shell**

-Including:'su'without a minus sign

-Only files of the 'bashrc' class will be loaded into

-When to start loading: when running 'bash'

-The loading sequence is as follows:

su root

loading ~/.bash_rc
loading /etc/bashrc
loaded /etc/bashrc
loaded ~/.bashrc

-This method is not complete and different from normal login environment, so it is not recommended



### /etc/profile

System environment initialization during system startup and terminal startup



### /etc/bashrc

Function and command aliases



##String processing

Variable default value dependent

Do not change the original variable value

${variable name – default value} variable is undefined, use default value
${variable name: – default value} # when the variable is empty, the default value is used. Empty includes undefined or empty (if it contains only one space, it will be empty)
${variable name: + default value} when the variable is not empty, the default value is used

Change the original variable

${variable name = default value} the variable is undefined, use the default value, and modify the original variable at the same time
${variable name: = default value} when the variable is empty, the default value is used and the original variable is modified

Direct error reporting

${variable name:? Prompt text} when the variable is empty, an error will be prompted

String operation

${variable name} string length

${variable name: pos:length }# extract length characters from position POS (subscript starts from 0)

#POS can be omitted, the default is 0
                      #Length can be omitted. The default is to the end of the string

${variable name # substring} # prefix matching, delete the matching string (non greedy mode)

#Substring the string to be deleted. Wildcards are supported

${variable name # substring} # greedy pattern

${variable name% substring} suffix matching, delete matching string (non greedy mode)
${variable name%% substring} greedy pattern

${variable name / substring / replace} matches all and replaces the first match
${variable name / / substring / replace} matches all and replaces all

The prefix of ${variable name / # substring / replace} is matched and replaced

${variable name /% substring / replace} # suffix matching and replacement

>The above match is a "wildcard" match pattern





For more information: https://linuxeye.com/390.html



##Array

**Define array**

Array name = (element1 Element2 element3)

be careful

Elements are separated by spaces. If the element itself contains spaces, quotation marks are needed
The adjacent positions in () are not limited to spaces
Display array

Print the first element

Echo $array name

Print all elements of array

Echo ${array name [@]}

Display the number of array elements

Echo ${# array name [@]}

Displays the first element of the array

Echo $array name

Displays an element of the array

Echo ${array name [n]} where n is the element subscript, starting from 0

Examples

cmdList=(
A1
A2
A3
)

for i in “${cmdList[@]}”; do

if $i; then
    echo "success"
else
    echo "error with code: $?"
fi

done

#Special symbols

##Other characters

-Comment character

-';' command separator

  -The separator used by the ';'; case statement

  >Connect multiple commands in a line, and each command is executed after the previous command is executed 
  >
  >The results of the previous task will not affect the subsequent tasks

-':' empty instruction (do nothing)

  Can be used in loop as a place holder, ':'always returns true (i.e. 0)

  >Because no statement is executed in the loop, an error will be reported

-Separate directory

  >` CP 123 {TXT, log} ` execution effect ` CP 123.txt 123.log`

-Conditional test

-'$' value symbol

  > ```sh
  >Echo $(command) # get the value of the running result
  > 
  >Echo ${variable name} takes the value of the variable
  >Echo ${variable name} take the variable length
  > 
  >Echo ${variable name [@]} takes the value of the array
  >Echo ${# variable name [@]} # takes the length of the array
  > 
  >Echo ${! Variable name} takes the variable value corresponding to the value of the variable name
  ># x = y; y = Z; echo ${! X} the result is Z
  > ```

-Pipe symbol

-'&' running in the background

-Special under shell

  -'. ` is equivalent to the source command

  -'~'home directory

  -'-'last directory

    `cd -`

  -'*' wildcard (any character)

  -'?' wildcard (1 arbitrary character)

  -Space



##Transferred meaning

-Common character escape gives different functions

  `\The escape of single letters n ','t','r '

-Special characters are used to escape to ordinary characters

  `\The escape of single non letter





##Citation

-Double quotation marks: * * * incomplete reference**

  Incomplete references explain variables in double quotes

A = $shell “# value is / bin / bash

-Single quotation mark: * * full reference**

Full reference , does not explain the variables

A = ‘$shell’ # value is $shell

-Reverse quotation mark: * * execute command**

Equivalent '$()`

a=whoami#The value is the current user name

##Brackets

The meaning of single use and non single use is usually different



-'()', ['(())'] (# simplification of double parentheses let command), '$()' parentheses

#When used alone, a subshell is generated
() # eg. (a = 123) when executing this command, it will not affect the current shell environment because it is executed in a child process (sub shell)

#Array initialization
Variable name = (array element)

#Arithmetic operator
(()) # short for the equivalent let command, eg. ((I + +)

#Execute the command and assign the result to the variable
Variable name = $(command) # equivalent to variable name=command

-Square brackets (test)

#Use alone, test
[] # equivalent test command, use – GT, – LT, etc. square brackets should be separated from the content. You can view the test results through $

#Test expression, [] enhancement, support extended syntax: & &, |, <, > and so on
[]] # the square brackets should be separated from the content. You can view the test results through $

-'<', '>' angle brackets (redirection))

Redirection symbol

-'{}' curly brackets (range, enumeration)

#Output range
echo {0..9} # 0 1 2 3 4 5 6 7 8 9

echo a{1,3,5} # a1 a3 a5

#File copy shortcut, etc
CP / etc / passwd {,. Bak} actually implements CP / etc / passwd / etc/ passwd.bak

#Scope
for i in {1..9}; do echo $i; done;

##Operators and logical symbols

-The arithmetic operators' + ',' - ',' * ',' / ','% '
-Comparison operators' > ',' < ',' = '
-Logical operators' & & ',' | ','! '





##Arithmetic operation

###Expr operation

Using 'expr' operation

Expr < operation part >

Examples

expr 4 + 5
A ='expr 4 + 5 '? Assign the result to the variable

be careful

Only integer is supported, floating point number is not supported
The value and operator must be separated by a space
###Let command

Let variable name = variable value

Examples

Let a = 10 + 20 # A is 30

be careful

Variable values can be mathematical expressions, including + - + - - - - + - = - = and so on
Variable values do not support floating point numbers
Variable value '0' starts with octal
The variable value '0x' starts with hexadecimal
>In fact, we seldom use 'let', but use simpler double parentheses
>
>It doesn't matter whether there is a space between the value and the operator



###Simplification of double bracket let command

[Click to view other brackets] (# brackets)

Double parentheses are a simplification of the let command

grammar

(())? Assignment / operation
$(()) ා reference calculation results

Examples

((a=10))
((a++))
((a--))
((a+=5))
((a=b=c=1))
Echo $((10 + 20)) # print results
B = $((1 + 2 + 3)) # reference result
#Test and judgment

##Exit and exit status

program

- `exit` 退出program, 返回状态以 `exit` 的上一条命令执行结果为准
- `exit <返回值>` 退出program, 返回状态以此处填写的 返回值(只能是数字)为准.





function

-'return 'the return status is subject to the last command execution result of' return '
- `return <返回值>` 退出program, 返回状态以此处填写的 返回值(只能是数字)为准



In general, the return value '0' indicates normal, others are abnormal exit



使用 `$?` 可以查看返回值, 用于确定 **当前Shell** 的上一个执行语句(进程, 脚本, function)是不是正常退出.





##Test command test

`It is equivalent to the test command, and it is more recommended to write it



`[]'Is an extended writing method of '[]', which supports' & & ',' | ',' < ',' >`

>If you want to use the characters' & & ',' | ',' < ',' > ', then * * must use' []]`

The test command detects files or compares values

  • Document testing
  • Numerical comparison test
  • String test

Return value description

True returns 0
False returns 1

grammar

Test expression
test
[expression]
[ ]

expression

Logical expression
    The expression 1 - A represents 2 # logic and
    Expression 1 - O expression 2 # logical or
    ! expression # logical not
    
String expression
    -Z str # zero, string length is 0
    -N str # non zero, string length is not 0
    STR1 = STR2 # string equality test (case sensitive)
    STR1! = STR2 # string inequality test (case sensitive)

Numerical expression
    -eq        # =
    -ge        # >=
    -gt        # >
    -le        # <=
    -lt        # <
    -ne        # !=

file
    不同file比较
        FILE1 -ef FILE2        # 相同file(同个设备, 相同inode)
        File1 - NT File2 # the modification time of file1 is newer than that of File2 (that is, the latest modification)
        File1 - ot File2 # is the opposite of - NT
    
    file
        -e FILE                # file存在
        -s FILE                # 长度大于0的file
    
    type            
        -f FILE                # 普通file(非下述几种type)
        -B file # block device
        -C file # char character device
        -D file # dir directory
        -h FILE                # 软连接file, 等同 -L
        -L FILE                # link 软连接file, 等同 -h
        -p FILE                # 命名管道file
        -S FILE                # socket file
    
    ascription
        -O FILE                # file有有效的属主            
        -G FILE                # file有有效的属组
    
    jurisdiction
        -r FILE                # 已设置读jurisdiction
        -w FILE                # 已设置写jurisdiction
        -x FILE                # 已设置执行jurisdiction
        
        -U file # suid (set user ID) set
        -G file # sgid (set group ID) set
        -K file # sbit (sticky bit set) set 
        
    other            
        -t FD                  # file描述符在终端上打开
###Extended usage of '[]]'

What is recorded here is different from ']'

expression

==# wildcard matching, support: *? 
=~# regular matching    
&&
||
<
>
>'- a', '- o' are not supported`





##Conditional if

Complete grammar

If [test condition]
Then execute the corresponding command # if the condition is true or the return value is 0, enter then, then does not need to be followed by a semicolon
Elif [test condition] #
Else execute the corresponding command
Fi # end

It can be written in one line

If [test condition]; then execute corresponding command; else execute corresponding command; fi

Debug if true

if :; then … fi

Debug if false

if [ ! : ]; then … fi

Determine the result of executing a command

If command; then

echo "success"

else

echo "fail"

fi

>If it is a command or an executed script or function, then it is to judge whether it is true according to the returned result value
>
>If statements support nested use





##Branch case

Complete grammar

Case “$variable” in

Case 1)
    command
;;
Situation 2)
    command
;;
*) # wildcards are used here to match the rest
    都不匹配时执行的command
;;

esac

The matching conditions support the following wildcards

- `*`
- `?`
- `|`
-'[]' indicates any character in the range, which can be used in the range`-`
-Adverse selection?
-Adverse selection?



>The above situations support wildcards, such as' * ',' | ','`
>
>   
>
>Note that the following two are different representations
>
>- '"case'"'Here, the 'case' is treated as a string, and the wildcards in it will not take effect
>- the wildcards in the case here take effect
>
>So the following matches are different
>
> ```sh
>Start | stop) # match start or stop
> 
>"Start" | "stop") # same as above, match start or stop
> 
>"Start | stop") # matches the entire string of "start | stop"
> 
>"CMD *") ා match the whole string of "CMD *"
> 
>"CMD?") ා matches the entire string of "CMD?"
> 
>CMD *) # wildcard matching CMD*
> 
>CMD?) wildcard matching CMD?
> ```



##Cycle

###For traversal

For parameter name in list
do

:

done

​    

**List source**

1. The list contains multiple variables (separated by spaces)

for i in 1 2 3

For I in {1.. 3} use curly braces to generate a list

2. Execute the command in the way of '` or' $(), line by line by default (if there is a space, it will be regarded as multiple lines)
 for file in `ls`
3. Enumerate paths (wildcards can be used)

#Enumerated is the full path, can be used with the basename command to obtain a short file name
for file in /etc/profile*
do

   echo $file

done

#The output is as follows
/etc/profile
/etc/profile.d

###For loop

C language style for command

grammar

For ((variable initialization; loop judgment condition; variable change))  
do
    command
done

Examples

for ((i=0;i<=10;i++)); do echo $i; done
>Not very common

 

###While loop

grammar

#If the conditions are met, it will be carried out
While test condition is true
do
    command
done

Examples

i=0
while [[ i<=10 ]]
do
    ((i+=3))
done
>It is often used to build interactive menus

 

It can be processed with shift command

If the offset number is n, the new value of $1 is the original value of ${n + 1}, and so on

Shift < offset = 1 >

Examples

while [ $# -gt 0 ] do
    shift 1
done
###Until loop

grammar

#Do not meet the conditions on the implementation
Until test condition is false
do
    command
done
###Break and continue

while :
do

break;
continue;

done

#Function

##Custom function

definition

Function name (){
    Local function local variable name; # variable scope is only inside the function
    echo $1;                   # 
}

be careful

Syntax requirements: the first curly bracket should be followed by a space (space, newline, etc.)

use

Function name
User defined functions can be put in a script file, and can be called in the current shell environment through 'source' or '

 

Examples

hello() { echo hello $USER; }

Hello # output Hello root

Example of sequential execution of multiple functions

A1 () { : }
A2 () { : }
A3 () { : }

cmdList=(
A1
A2
A3
)

for i in “${cmdList[@]}”; do

if $i; then
    echo "success"
else
    echo "error with code: $?"
fi

done

##Get function name

There are some predefined variables inside the function

Array of $funcname, containing the call stack name

Examples

!/bin/bash

abc() {

echo "in abc: ${FUNCNAME[@]}"
ef

}

ef() {

echo "in ef: ${FUNCNAME[@]}"

}

abc

Output content

in abc: abc main
in ef: ef abc main

##Delete function

Unset – F function name

##System script

Self built function library: '/ etc / init.d/functions`





#Script control

##Script priority control

You can use nice and renice to adjust the script priority



CPU calculation and creating subprocess will cause system overhead



Creating a dead loop consumes a lot of CPU and results in a crash

1. When and for create a dead loop, the CPU consumption will be too high,   

2. Fork bomb: the program creates a large number of subprocesses, and the CPU does not respond to any information

#Example – define a func function
func () { func | func& }

#After calling, the system will create a sub process crazily. At this time, Ctrl + C is no longer effective
func

#Another common fork bomb
.(){ .|.&}; .

`The output of ulimit - a '

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 5642
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
Max user processes (- U) 5642 ᦇ number of child processes that can be created at the same time, root is invalid
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

>Most of the restrictions do not apply to root



##Capture signal

`By default, it sends signal No.15 (SIGTERM) to the application

`Send SIGINT signal to application program

Sigkill signal can't be captured, can't be blocked, force to kill the process



Set capture signal

Trap signal

< parameters > refers to the processing after the signal is captured, which can be divided into the following situations

The < parameter > is not set, and it will be processed by default
The value is - according to the default processing, as above
If the value is empty, the signal will be captured and ignored directly
Value for other capture signals and execute the specified command

Examples

#Capture SIGINT (2) signal and print "got signal 2"
trap "echo got signal 2" 2
>The capture signal can be set in important non - interruptible scripts to avoid being terminated by accident, such as backup scripts







Control group? Control memory?



#Planning tasks

##At command

Set up one time scheduled tasks

After entering the task to be executed, you need to append Ctrl + D to indicate that the input is completed

At time

Time format

HH: mm # hour: minute specify the next time point (today or next day)
HH: mm today
HH: mm tomorrow
HH: mm mmddyy # hour: minute month day day, year can be 2-digit abbreviation, or 4-digit full write
HH:MM MM/DD/YY
HH:MM MM.DD.YY
HH:MM YYYY-MM-DD

Now + count # offset of current time point, counting unit: minutes, hours, days, weeks. Eg. 4pm + 3 days

option

-C < serial number > # view the specific task execution content
-F < File > # read command from specified file instead of standard input

be careful

1. The full path should be used for Non internal commands, and source should be used for shell scripts to introduce environment variables
2. There is no terminal for the execution of the planned task, so there is no standard output, so you need to redirect the output by yourself

Tip
In view of the inconvenience of at in setting command, we can consider setting it together with cat, eg

Cat <'eof '| at time
Specific command to be executed
EOF
be careful

-One time tasks depend on ATD services
-The 'at' directory takes the directory in which the task is set as the working directory. Therefore, if the directory is invalid (deleted or restricted), the task execution will fail
-The root user can use the at command in any case, while the permissions of other users using the at command are defined in / etc/ at.allow (users allowed to use scheduled tasks) and / etc/ at.deny (users who are refused to use the scheduled task) there is no file by default, and you need to create the allow user and reject user files by yourself;
  -If / etc/ at.allow The file exists. Only the user corresponding to the user name in the file can use at;
  -If / etc/ at.allow File does not exist, / etc/ at.deny Exist, all not in / etc/ at.deny Users in the file can use at;
  -  at.allow than at.deny The priority is high. Whether the executing user can execute the at command is up to you first at.allow Is there anything in the file at.deny Documents;
  -If / etc/ at.allow And / etc/ at.deny If none of the files exist, only root can use at;



**Atq command**

Query the queue of one-time scheduled tasks waiting to be executed

The number on the left is the task ID of the planned task

atq

**ATM command**

Remove unexecuted scheduled tasks

ATM task serial number

##Periodically scheduled tasks Cron

Crontab [options]

option

-Edit to enter the VIM editor 
-View configured items

Configuration format

Time sharing day month week command
*Arbitrary
1,2,3 # comma separated
1-3 # equivalent representation 1,2,3

be careful

The command should use the full path
Configuration file (each user has its own configuration)

`/Var / spool / cron / user name`



Crond related logs (excluding task output)

`/var/log/cron`



##Schedule tasks and locks

###Anacron periodic command scheduler

Anacron is a tool for executing commands periodically (it is suitable for non 24-hour power on and needs to ensure that the specified tasks are run daily / weekly / monthly)

Its time granularity is "days". For example, if logrotate is configured to run once a day, it will try to run the task every hour between 3 and 22 o'clock by default (so the host does not have to be turned on 24 hours)



**Suitable for * *: for scripts that need to be executed at least once on a regular basis, anacron can be used to configure scheduled tasks

>For example, it is required to execute at least once every three days, but there is no strict requirement on which day and which hour



**The process of calling anacron by crond service**

1. The crond service performs the scheduled tasks configured in the directory '/ etc / cron. D /' (crontab format) every minute

   -'/ etc / cron. D / 0hourly' configure the first minute of every hour to execute '/ etc'/ cron.hourly/ `All * * < U > scripts in the directory**
   -'/ etc / cron. D / sysstat' is configured to collect system performance statistics every 10 minutes and generate a daily report near midnight every day

2. `/etc/ cron.hourly/0anacron `Script

   >Main logic of the script: if anacrontab has not been executed today, the command '/ usr / SBIN / anacron - s' will be executed
   >
   >It means to execute delayed scheduled tasks sequentially (non parallelly)





**The execution process of anacron**

1. Read the configuration file '/ etc / anacrontab`

# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

2. Perform the tasks configured above in turn

Anacron will perform the daily / weekly / monthly tasks configured above with a random delay of 0-45 minutes from 3:00 to 22:00 every day

- `/etc/ cron.daily/logrotate `Log rotation tool call
- ...





###Flock lock file

Flock [option]

Examples

Flocak - xn "/ TMP / f.lock" - C "command or script to execute"

option

Lock type
-Exclusive lock


-n. -- Nb, - Nonblock # exit directly when locking fails (default is wait)

-c. -- command < command >? Command to be executed

be careful

After the lock file is deleted, it will be invalid
#Others to be sorted out

##

#Run parts command

Run all executable files in the specified directory (with execution permission)

-It can be configured in the directory jobs.deny  And jobs.allow  To configure the black / white list
-It is often used for crond timing tasks to execute all scripts in a directory

Run parts < Directory >

##Mktemp command

Create a temporary file or directory

Mktemp [option] [template]

Template

It should contain at least three consecutive "XS". If not specified, it will be created in the / tmp directory tmp.XXXXXXXXXX

option

-d. -- directory # create a directory (instead of the default file)
-T # create a temporary file in the / tmp directory

Examples

mktemp                    # /tmp/tmp.CaE8KvS8HI
mktemp  log.XXXXXXX         # log.KyvESFk             In the current directory
mktemp -t log.XXXX        # /tmp/log.RNwC
##Yes command

Output the specified string on the command line until the yes process is killed

Yes [option] < string = Y >

Examples

Common expressions need simple interaction: enter Y / yes to continue
Yes | Yum install... # this is just an example, but the actual Yum is generally combined with - Y

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]