Shell basics-day2

1. The difference between using in bash;, &&, ||.

    ; Does not have a logical judgment function, no matter whether the previous command is successful or not, the following commands will be executed in turn, shell commands, if there is more than one in a line, you need to add; or && or ||

    && and || have a logical judgment function. Use && to connect, only the previous command will execute successfully and the subsequent commands will be executed. If you use || to connect, the following commands will only be executed if the previous command fails. ping baidu.com && echo "is up" || echo "is down" When ping baidu.com is executed successfully, it will output is up or it will output is down. In bash, 0 represents true, and non-zero represents false.

2. Wildcards in the shell

    * Match any number of characters

    ? Match any character

    [] Match any character in the brackets [abc][az][^az]

    () Execute in the subshell (cd) 

    {} is the meaning of collection touch file{1...9}, this statement creates 9 files

    Escape character \

3. Custom variables in shell scripts

      The naming of variables is the same as that of C language.

      Variable name = variable value, so a variable is defined. When the value of this variable is needed, use $variable name or ${variable name} .

      Note that this kind of thing is hard to write, it can also be written as read from the keyboard, and it can be written as the name of the read variable. So there is no prompt, you can add a prompt and write the command as read -p "please input a variable name: "variable name .

      More special variables $1, $2... Note that this variable is a parameter when the script is executed, where $1 is the first variable when the script is executed, and $2 is the second variable when the script is executed.

      You can include all the content in another script in a script, just add the path of the script to the script.

4. Environment variables in the shell

    You can use the export keyword to define environment variables, export variable name=variable value, or directly export existing variables. Note that the scope of influence of custom variables is in the current shell, while the scope of influence of environment variables is in the current shell and sub Take effect in the shell.

    You can use the env command to see which environment variables are defined in the system.

5. Predefined variables in the shell

    $0 script name, note that this name has a path, if you want to get the name of the file, you can basename $0

    $* all parameters

    $@ all parameters

    $# Number of parameters

    $$ PID of the current process

    $! PID of the last background process

    $? The return value of the previous command 0 means success

6. In the shell, '' is a strong reference, and "" is a weak reference. The effect is shown in the figure below:

7, the operation of variables

     1) Integer operations

          Method 1: expr   

                       The output of expr 1 + 2 is 3, and the output of echo 1 + 2 is the string 1 + 2

                       expr $num1 + $num2 The output is the value added by the variables

          Method 2: $(()) and $[]

                       echo $((3*5)) Note that the multiplication sign here does not need to be escaped

          Method three: let 

                        let sum = 2 + 3; echo $sum

                        let i++; echo $i 

8. Deleting, replacing and slicing variable "content"

command meaning Show results
echo ${url}   Output the content of the variable url   www.sina.com.cn
echo ${url#www.}   The output variable url deletes the content after "www.", but the content of the url remains unchanged. Only the output display content has changed. sina.com.cn
echo ${url#*.} The output shows that the URL has deleted the remaining characters of the previous characters including the first. But the content of the url has not changed. Only the output display content has changed. sina.com.cn
echo ${url##*.} The output shows that the URL has deleted the remaining characters of the previous characters including the last. But the content of the url has not changed. Only the output display content has changed. cn
echo ${url%.*} Delete from back to front to the first. The remaining content after the output. www.sina.com
echo ${url%%.*} Delete from back to front to the last. The remaining content after the output. www
echo ${url:5:5} Intercept the output of 4 characters from the 5th character (including). ina.c
echo $ {url / sina / baidu} Replace the first sina in the url with baidu output www.baidu.com.cn
echo $ {url // n / N} Replace all n in the url with N output www.siNa.com.cN

      Note that for the various operations recorded above, the value of the variable url has not changed, but it has been changed during output.

      Note the syntax: ${variable name-new variable value} If the variable has not been assigned, it will be replaced with "new variable value"; the variable is assigned (including null value): it will not be replaced.

                         ${Variable name:-new variable value} If the variable has not been assigned (including a null value), it will be replaced with "new variable value"; the variable is assigned: it will not be replaced.

9. The meaning of various strange symbols in the shell

     () Execute in a subshell

     (()) Numerical comparison, calculation

     $() command substitution

      $(()) integer arithmetic

      {} collection

      ${} variable reference

      [] Condition test

      [[]] Condition test. Support regular=~

      $[] integer arithmetic

10,

11. After reading these two blogs, you should master the following knowledge points.

    

Guess you like

Origin blog.csdn.net/xiaoan08133192/article/details/108875067