Shell programming conditional test statement format plus simple case

Conditional test

  • test command
    test whether the expression is established, if it is established, return 0, otherwise return other values
    Format 1: test conditional expression
    Format 2: [conditional expression]
    Insert picture description here
    Insert picture description here

File test

  • File test format
    [operator file or directory]
  • Commonly used test operators
    • -d : test is whether the directory (Directory)
    • -e : Test whether the directory or file exists (Exist)
    • -f : test whether it is a file (File)
    • -r : Test whether the current user has permission to read (Read)
    • -w : Test the current user is whether it has permission to write (Write)
    • -x : Test the current user is whether it has permission to perform (eXcute)

Integer test

  • Integer value comparison format
    [integer 1 operator integer 2]
  • Commonly used test operators
  • -eq: equal to (Equal)
  • -ne: Not Equal
  • -gt: Greater Than
  • -lt: Less than (Lesser Than)
  • -le: Less than or equal to (Lesser Equal)
  • -ge: Greater than or equal to (Greater Equal)

String test

  • String comparison
    • Format 1: [String 1 = String 2]
    • Format 2: [-z string]
  • Commonly used test operators
    • = : The string content is the same
    • != : The string content is different,! Means the opposite
    • -z : The string content is empty
    • -n : check if there is a string
      Insert picture description here

Logic test

  • Logic test
    • Format 1: [Expression 1] Operator [Expression 2]…
    • Format 2: Command 1 Operator Command 2…
  • Commonly used test operators
  • -aOr &&: logical and, meaning "and". The previous execution is successful and the execution continues, and the latter is not executed without success.
  • -oOr ||: logical or, meaning "or". Or if the previous execution is successful, the latter will not be executed, and the previous execution will fail to execute the latter
  • !: No Logical
    a = 5
    [ $a -ne 1 ] && [ $a !=2 ]equivalent [ $a -ne 1 -a $a !=2 ]
    &&and ||the operator can be present in a normal [[ ]]condition judging structure, but if present in [ ]being given structure

if statement

if single branch statement

  • Single branch structure
    Insert picture description here
    Insert picture description here
  • For example

Insert picture description here

if double branch statement

  • Double branch structure
    Insert picture description here
    Insert picture description here
  • For example

Insert picture description here

if multi-branch statement

  • Multi-branch structure
    Insert picture description here
    Insert picture description here
  • For example
    Insert picture description here

case branch statement

  • case multi-branch structure
    Insert picture description here
    Insert picture description here
  • Precautions
    • The case line must start with the word "in", and each mode must end with a single right parenthesis ")"
    • The double semicolon ";;" indicates the end of the command sequence
    • In the pattern string, you can use square brackets to indicate a continuous range, such as "[0-9]"; you can also use vertical bars | to indicate or, such as
      the * at the end of a|b to indicate the default mode, where * is equivalent to Wildcard
  • For example:
    Insert picture description here
  • The second way
    Insert picture description here
  • Example 2
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_53496398/article/details/114364711