Shell logic judgment, file attribute judgment, if special usage, case judgment

Source: Internet
Author: User
Tags case statement

Logical-judgment logical expressions in shell scripts are used in [] (brackets):
    • -lt:=little than less than
    • -le:=little && wqual less than equals
    • -eq:=equal equals
    • -ne:=no Equal Not equal to
    • -gt:=greater than greater than
    • -ge:=greater && equal greater than or equal
Used in (()) parentheses:
    • <,<=,==,!=,>,>=

You need to enclose it in double brackets.

Format One
    • if condition; then statement; fi

      #!/bin/bash
      A=5
      If [$a-GT 3]
      Then
      echo "OK"
      Fi
      ~
      [] There are spaces between the variable and the parentheses and the logical expression

Format Two
    • if condition; Then statement 1;else statement 2;fi

      #!/bin/bash
      A=5
      If [$a-GT 3]
      Then
      echo "OK"
      Else
      echo "Fault"
      Fi

Format three
    • If condition, then statement 1;elif condition 2;then Statement 2;else statement 3;fi

      #!/bin/bash
      A=5
      If [$a-lt 3]
      Then
      echo "A<3"
      elif [$a-GT 6]
      Then
      echo "A>6"
      Else
      echo "Nook"
      Fi

Relationship
    • The relationships between the various conditions can use logical connectors
      • Conditions a&& conditions b:a and B
      • Condition a| | Condition B:a or B
File Directory property judgment
    • The IF in a shell script is often used to determine the properties of a document, such as whether it is a normal file or a directory file, determine if the file has read and write execution permissions, etc.
File Directory Property Determination options
    • -E: Determine if a file or directory exists
    • -D: Determine if the directory file is not present
    • -F: Determine if the file is not normal and whether it exists
    • -r: Determine if Read permission is available
    • -W: Determine if Write permission is available
    • -X: Determine if EXECUTE permission is available

      #!/bin/bash
      f= "/tmp/test/123456"
      If [-e $f]
      Then
      echo "OK"
      Else
      echo "No"
      Fi
      Determine if the file or directory exists, there is a return OK, there is no return no

If special usage
  • If [-Z ' $a]: Indicates what happens when the value of variable A is empty

  • If [-n ' $a]: Indicates what happens when the value of variable A is not empty

  • -N and-Z do not work on the file. Can only be used for variables.

  • -Z and-N are the opposite two conditions

    #!/bin/bash
    n=wc -l /tmp/test.txt
    If [$n-gt 20]
    Then
    Echo 1
    Else
    Echo 0
    Fi
    There is no syntax error in the script, but we preset/tmp/test.txt to exist.
    If the file does not exist, the script will error when it executes

    [Email protected] shell]# sh file.sh
    WC:/tmp/test.txt: No file or directory
    if.sh: Line 3rd: [:-gt: Expecting unary expression

    So, in order to avoid this kind of error, we need to write the script more rigorous
    You need to confirm the existence of the file "/tmp/test.txt" before executing "if [$n-gt 20]"

    #!/bin/bash
    n=wc -l /tmp/test.txt
    If [-Z $n]
    Then
    echo "Error"
    Exit
    If the file does not exist, the script will be rolled out here.
    The following will not be executed
    elif [$n-lt 20]
    Then
    Echo 1
    Else
    Echo 0
    Fi

    Execution Result:
    [Email protected] shell]# sh if.sh
    WC:/tmp/test.txt: No file or directory
    Error

  • If grep ' 123 ' test.txt;then indicates what happens when the test.txt contains 123

    To determine that a parameter does not exist
    #!/bin/bash
    If
    Grep-wq ' user1 '/etc/passwd
    Then
    echo "User1 exist."
    Fi
    [Email protected] sbin]# sh if1.sh

    To determine that a parameter does not exist:
    #!/bin/bash
    If
    ! Grep-wq ' user1 '/etc/passwd
    Then
    echo "No User1"
    Fi

    -W: Exact match, filter a word
    -Q: Quiet mode, do not print filter results
    ! : Take counter

    grep non-quiet mode:
    [Email protected] shell]#./file.sh
    User1:x:1005:1006::/home/user1:/bin/bash
    User1 exist.

    grep Quiet mode:
    [Email protected] shell]#./file.sh
    User1 exist.

Case Judgment
  • The case statement is a multi-select statement, which can match a value with a pattern, and execute a matching command if the match succeeds.

    Case value ($ variable name) in
    value1)
    Command
    ;;
    value2)
    Command
    ;;
    VALUE3)
    Command
    ;;
    *)
    Command
    ;;
    Esac

  • The case works as shown above, the value must be followed by an in, and each pattern must end with parentheses. The value will detect each pattern that matches. Once the pattern is matched, other modes are no longer resumed after the corresponding command is executed.

  • If there is no match to any one of the modes, the command after the * mode is executed.

  • In a case, you can use |, the meaning of the expression or in the condition

    2|3)
    Command
    ;;

  • Instance

    #!/bin/bash
    Read-p "Please input a number:" N
    If [-Z "$n"]
    Then
    echo "Null,please input a number."
    Exit 1
    Exit 1 indicates the return value after executing the partial command
    That is, when the command finishes executing, use echo $? The value
    Fi

    n1=echo $n | sed ‘s/[0-9]//g‘
    Change the number in variable N to null and assign to N1
    If [-N "$n 1"]
    Determines if the N1 is not empty, then outputs the following result, and is empty to continue execution
    Then
    echo "Please input a number."
    Exit 1
    Fi

    If [$n-lt] && [$n-ge 0]
    Then
    Tag=1
    elif [$n-ge] && [$n-LT 80]
    Then
    tag=2
    elif [$n-ge] && [$n-LT 90]
    Then
    Tag=3
    elif [$n-ge] && [$n-lt 100]
    Then
    Tag=4
    Else
    Tag=0
    Fi
    tag is the variable name, as a label for easy reference.
    Case $tag in
    1)
    echo "D"
    ;;
    2)
    echo "C"
    ;;
    3)
    echo "B"
    ;;
    4)
    echo "A"
    ;;
    *)
    echo "The number is range 0-100"
    ;;
    Esac

    Case $tag in
    1)
    echo "D"
    ;;
    2)
    echo "C"
    ;;
    3)
    echo "B"
    ;;
    4)
    echo "A"
    ;;
    *)
    echo "The number is range 0-100"
    ;;
    Esac

    The script is to enter the test scores and determine the grade of the test scores.

  • Test

    [Email protected] shell]#./case.sh
    Please input a number:95
    A
    [Email protected] shell]#./case.sh
    Please input a number:85
    B
    [Email protected] shell]#./case.sh
    Please input a number:75
    C
    [Email protected] shell]#./case.sh
    Please input a number:55
    D
    [Email protected] shell]#./case.sh
    Please input a number:101
    The number is range 0-100
    [Email protected] shell]#./case.sh
    Please input a number:7y
    Zm,please input a number.
    [Email protected] shell]#./case.sh
    Please input a number:
    Null,please input a number.

Shell logic judgment, file attribute judgment, if special usage, case judgment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.