5. Shell command script

Shell script

  • The first line must have the statement "#!/bin/bash" to tell what is needed
  • Standard code, must have standard comments
  • Some built-in variables:
    $0-file name
    $#-number of parameters
    $*-what are the parameters
    $<num>-the <num>th parameter
  • Test sentence-[…]

    (执行完成之后,再执行echo$?,若返回的结果为0,则上一条语句执行成功)测试语句的中括号内部,首尾必须有空格,否则报错。
  • The first line must have the statement "#!/bin/bash" to tell what is needed

  • [-d]-Test whether the file is a directory type

  • && If the previous statement is executed successfully, execute the following statement

  • || If the previous statement fails, the following statement is executed

  • When comparing numeric types, you should use specific comparison characters [-eq] [-ge] [-gt] [-le] [-lt]

  • [-z]-Test whether the variable is empty (occupied)

  • The negative test character "!" is used in the first part of the brackets (instead of "!=")

  • if conditional statement

  • The single-branch structure of the f conditional statement is composed of if, then, and fi keywords, and the preset command is executed only after the condition is satisfied, which is equivalent to the colloquial "if... then...". The single-branch if statement belongs to the simplest kind of conditional judgment structure.
    5. Shell command script

  • The double-branch structure of the if conditional statement is composed of if, then, else, fi keywords. It performs a condition matching judgment. If it matches the condition, it executes the corresponding preset command; otherwise, it executes the preset when it does not match. The command is equivalent to the colloquial "if...then...or...then...". The double-branch structure of if conditional statement is also a very simple judgment structure.
    5. Shell command script
  • The multi-branch structure of the if conditional statement consists of if, then, else, elif, fi keywords. It performs multiple condition matching judgments. Any one of these multiple judgments will execute the corresponding preset command after the match is successful. It is equivalent to the colloquial "if...then...if...then...". The multi-branch structure of if conditional statements is the most commonly used conditional judgment structure in work, although it is relatively complex but more flexible.

    5. Shell command script

  • loop statement

1. For loop → specify a certain range

2. While loop → specify certain conditions

3. The for loop statement allows the script to read multiple information at once, and then manipulate the information one by one
5. Shell command script

  • The while conditional loop statement is a statement that allows the script to repeatedly execute commands based on certain conditions. Its loop structure is often not sure about the number of final executions before execution.
    5. Shell command script
    /dev/null is a file called Linux black hole. Redirecting output information to this file is equivalent to deleting data (similar to a trash can without recycling function), which can keep the user's screen window simple.
    In a Linux system, /etc/passwd is a file
    case condition used to save user account information The functions of the test statement and the switch statement are very similar! The case statement is to match data in multiple ranges. If the match is successful, the related command is executed and the entire condition test is ended; if the data is not in the listed range, the default command defined in the asterisk (*) will be executed5. Shell command script

to sum up:

  • if single branch, double branch, multiple branch

  • for range

  • while condition

  • case judgment
    5. Shell command script5. Shell command script

Guess you like

Origin blog.51cto.com/14846455/2597036