linux - shell conditional judgment statements, files, strings, numbers

1: Conditional judgment syntax format     Forma...

1: Conditional judgment syntax format

    Format 1: test conditional expression

    Format 2: [conditional expression  ]

    Format 3: [[conditional expression  ]] Support regular=

    Note: check the conditional judgment of the test expression through man test

2: Judge the file type (the following are common)

     - e     Determine whether the file exists

     - f     Determine whether the file exists and is an ordinary file

    - d     Determine whether the file exists and is a directory

    - L     Determine whether the file exists and is a soft connection

    - b     Determine whether the file exists and is a device file

    - S     Determine whether the file exists and is a socket file

    - c       Determine whether the file exists and is a character device file

    - p     Determine whether the file exists and is a named pipe file

    - s       Judge whether the file exists and is a non empty file (with content)

2: Judge file permissions   (the following are common)

     - r         Is the current user readable

    - w         Is it writable by the current user

    - x         Is the current user executable

    - u         Is there a suid   Advanced rights adventure bit

    - g         sgid advanced permission enforcement bit

    - k           Is there a t-bit advanced permission sticky bit to control deletion

3: Judge whether the document is old or new

    file1 -nt file2             Compare whether file1 is newer than File2

    file1 -ot file2             Compare whether file1 is older than File2

    file1 -ef file2             Compare whether it is the same file or whether the hard connection points to the same inode node

4: Judgment integer

    - eq             equal

    - ne             Unequal

    - gt               greater than

    - lt                 less than

    - ge               Greater than or equal to

    - le               Less than or equal to

5: Interpretation string

    - z                   Judge whether it is an empty string. If the string length is 0, it is true

    - n                 Judge whether it is a non empty string. If the string length is not 0, it is true

  str1 = str2       Determine whether the strings are equal

  str1 != str2     Judge whether the strings are unequal

6: Multiple panduan

   - a and&&   Both logic and  

      Use - A   [ 1 -eq 1 -a 1 -ne 0 ]  

      Use & & [1 - EQ 1] & & [1 - Ne 0]

Comprehensive actual combat:

Example 1:1: preach a test04 file directory in the / home/shell file directory,

              2: Create test1.txt and test2.txt files in the test04 directory and save them in the two files

              3: Write I am test1.txt and I am test2.txt

              4: Create test04.sh in the / home/shell directory

              5: The script is as follows:

                  5.1: whether the output test04 file exists,

                  5.2: output test04   The file exists and is a directory

                  5.3: judge whether test04/test1.txt exists

                  5.4: judge whether test04/test2.txt has content

                  5.5: does the current user have permission to execute test04.sh

                  5.6: compare text1.txt and test2.txt

                  5.6: define two variables str1="hellworld" and str2="hellowold"  

                        Determine whether str1 and str2 are equal

              The above script is as follows:

#!/bin/env bash # Author: lvdqiang # Desc: test file judgment # Using: execute the command 1: chmod +x /home/shell/test04.sh # Execute command 2: / home/shell/test04.sh # Determine whether the file exists echo "-----------judge test04 Does the file exist----------" test -e /home/shell/test04 A=$(echo $?) if [ $A -eq 0 ] then echo "existence" else echo "non-existent" fi echo "-----------judge test04 Does it exist and is a directory----------" test -d /home/shell/test04 B=$(echo $?) if [ $B -eq 0 ] then echo "It's a directory" else echo "Not a directory" fi echo "-----------judge test04/test1.txt Does it exist----------" test -f /home/shell/test04/test1.txt C=$(echo $?) if [ $C -eq 0 ] then echo "Is there" else echo "non-existent" fi echo "-----------judge test04/test1.txt Is there any content----------" test -s /home/shell/test04/test1.txt C=$(echo $?) if [ $C -eq 0 ] then echo "Is there" else echo "non-existent" fi echo "-----------judge test04/test2.txt Is there any content----------" test -s /home/shell/test04/test2.txt C=$(echo $?) if [ $C -eq 0 ] then echo "Is there" else echo "non-existent" fi echo "-----------judge test04.sh Do you have execution permission----------" test -x /home/shell/test04.sh C=$(echo $?) if [ $C -eq 0 ] then echo "Is there" else echo "non-existent" fi echo "-----------judge test1.txt Is it better than test2.txt new----------" test /home/shell/test04/test1.txt -nt /home/shell/test04/test2.txt C=$(echo $?) if [ $C -eq 0 ] then echo "new" else echo "Not new" fi str1="hellworld" str2="helloworld" echo "-----------str1="hellworld" and str2="hellowold" judge str1 and str2 Are they equal----------" test str1 = str2 C=$(echo $?) if [ $C -eq 0 ] then echo "equal" else echo "Unequal" fi

The above implementation results are shown in the figure below:

12 November 2021, 13:46 | Views: 3305

Add new comment

For adding a comment, please log in
or create account

0 comments