Shell basics-day3-condition test

0, there are 3 types of condition tests: file test, numerical comparison, string comparison

1, test conditional expression

      Note that this grammar will not return an error, but will only return the corresponding value. See the picture below for a specific example. test -d /home Determines whether the directory /home exists, returns true if it exists, and returns false if it does not exist.

      The meaning of the following script is to create the directory if the directory /var/mysql_back does not exist.

#!/usr/bin/bash
back_dir=/var/mysql_back
if ! test -d $back_dir; then
    mkdir -p $back_dir
fi

2. [Conditional expression]

      It should be noted that the functions of [-d /home] and test -d /home are exactly the same. Different tests can be directly followed by expressions, and the left square bracket of [] is the command for testing (note that [is A command), the right square bracket] is a required parameter for [square bracket. In fact, remember that [conditional expression] is the function of testing.

     File test [\d directory] Test whether the directory exists

     Numerical comparison [$variable name-eq|-ne 0] -eq test the previous variable is equal to 0, -ne test the previous variable is not equal to 0

     String comparison [$variable name== string] Test to determine whether the variable name is equal to the string

3. [[Conditional expression]]

      Similar to [expression]. [[]] and [] have different syntax when combining or ORing the two sentences. [Statement 1 -a Statement 2] [[ Statement 1 && Statement 2]] Is the test statement 1 and statement 2 true? [Statement 1 -o Statement 2] [[ Statement 1 || Statement 2]] Is one of the test statement 1 and statement 2 true?

4. File testing

      [-e dir|file] Test whether the specified directory or file exists 

      [-d dir] Test whether the specified directory exists

      [-f file] Test whether the file exists

      [-r file] Test whether the current user has read permission to the file

      [-x file] Test whether the current user has execute permission on the file

      [-w file] Test whether the current user has write permission to the file

      [-L file] Test whether the following file is a connection

5. Numerical comparison

      [$A -gt 10] Test whether A is greater than 10

      [$A -lt 10] Test whether A is less than 10

      [$A -eq 10] Test whether A is equal to 10

      [$A -ne 10] Test whether A is not equal to 10

      [$A -ge 10] Test whether A is greater than or equal to 10

      [$A -le 10] Test whether A is less than or equal to 10

      Note that the above grammar may not be easy to remember, you can use c-style grammar. Use (()) to execute the judgment statement in parentheses.

6, string comparison

     Tip: Use double quotes

     For example, the command ["$USER" == "root" ]; echo $? means that if the current user is root, output 0, otherwise output 1. It should be noted here that if the previous command is written as [$USER == root ], a syntax error may be reported, so get used to using "" to cause string variables to avoid unnecessary trouble.

     [-z "$var"] Is the length of the test variable var 0?

     [-n''$var"] Is the length of the test variable var not 0?

Command summary:

    id username; what is returned is the user's information, if there is no such user, an error will naturally be reported.

    bash -n .sh file; check the syntax of this script file.

 

 

Guess you like

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