linux judgment command test solution

The judgment command test can be abbreviated as brackets []. It can be divided into string judgment and general judgment.
Note that there must be spaces between the brackets [] and the judgment conditions, and between the judgment symbols of the judgment conditions and the comparison value.

If test is used alone, if the judgment condition is true, enter echo $? 0 will be returned. If the judgment condition is false, any number between 1 and 255 will be returned.

(1) Numerical judgment

Judgment symbol explain example
-eq Equal is true [ $a -eq $b ]
-ne Unequal is true [ $a -ne $b ]
-gt Greater than is true [ $a -gt $b ]
-ge Greater than or equal to is true [ $a -ge $b ]
-lt Less than is true [ $a -lt $b ]
-le True if less than or equal to [ $a -le $b ]

Operation example:

[root@youxi1 ~]# vim a.sh
#!/bin/bash
read -p "Please enter the first number:" num1
read -p "Please enter the second number:" num2
if [ $num1 -eq $num2 ] ; then
    echo "The first number is equal to the second number"
elif [ $num1 -gt $num2 ] ; then
    echo "The first number is greater than the second"
elif [ $num1 -lt $num2 ] ; then
    echo "The first number is less than the second"
else
    echo "Judgment failure"
fi
[root@youxi1 ~]# sh a.sh
 Please enter the first number:1
 Please enter the second number:2
 The first number is less than the second
[root@youxi1 ~]# sh a.sh
 Please enter the first number:2
 Please enter the second number:1
 The first number is greater than the second
[root@youxi1 ~]# sh a.sh
 Please enter the first number:1
 Please enter the second number:1
 The first number is equal to the second number
[root@youxi1 ~]# sh a.sh
 Please enter the first number:aa
 Please enter the second number:bb
a.sh: Line 4:[: aa: Expected integer expression
a.sh: Line 6:[: aa: Expected integer expression
a.sh: Line 8:[: aa: Expected integer expression
 Judgment failure

(2) String judgment

Judgment symbol explain example
= Equal is true [ $a = $b ]
!= Unequal is true [ $a != $b ]
-z True if string length is 0 [ -z $a ]
-n True if string length is not 0 [ -n $a ]
> Greater than is true [ str1 > str2 ]
< Less than is true [ str1 < str2 ]

Operation example:

[root@youxi1 ~]# vim a.sh
#!/bin/bash
read -p "Please enter the first string:" str1
read -p "Please enter the second string:" str2
if [ $str1 = $str2 ] ; then
    echo "The two strings are the same"
elif [ -n $str1 ] && [ -n $str2 ] ; then
    echo "Two strings are not empty"
fi
[root@youxi1 ~]# sh a.sh
 Please enter the first string:aaa
 Please enter the second string:bbb
 Two strings are not empty

Operation example 2: note that when using string size comparison: 1 The order of strings, greater than or less than, is different from sort sorting; 2. Greater than and less than must be escaped; 3. In the test comparison test, the ASCII table is used, and the upper case letters are smaller than the lower case letters.

[root@youxi1 ~]# vim a.sh
#!/bin/bash
read -p "Please enter the first string:" str1
read -p "Please enter the second string:" str2
if [ $str1 \> $str2 ] ; then
    echo "The first string is larger than the second string"
else
    echo "The first string is smaller than the second string"
fi
[root@youxi1 ~]# sh a.sh / / prove that the comparison is not the sum
 Please enter the first string:ab
 Please enter the second string:ba
 The first string is smaller than the second string
[root@youxi1 ~]# sh a.sh / / the following two comparisons prove that they are word for word
 Please enter the first string:aa
 Please enter the second string:ab
 The first string is smaller than the second string
[root@youxi1 ~]# sh a.sh
 Please enter the first string:aa
 Please enter the second string:ba
 The first string is smaller than the second string

(3) Document judgment

Judgment symbol explain example
-e True if the file or directory exists [ -e file ]
-r True if the file exists and is readable [ -r file ]
-w True if the file exists and can be written [ -w file ]
-x True if the file exists and is executable [ -x file ]
-s True if the file exists and at least one character exists [ -s file]
-d True if the file exists and is a directory [ -d file ]
-f True if the file exists and is a normal file [ -f file ]
-c True if the file exists and is a character file [ -c file ]
-b True if the file exists and is a block special file [ -b file ]
-nt If file1 is newer than file2, it is true [ file1 -nt file2 ]
-ot If file1 is older than file2, it is true [ file1 -ot file2 ]
-ef True if file1 and file2 are the same file [ file1 -ef file2 ]

Operation example:

[root@youxi1 ~]# vim a.sh
#!/bin/bash
read -p "Please enter the first address:" -r addr1
read -p "Please enter a second address:" -r addr2
if [ $addr1 -nt $addr2 ] ; then
    echo "New document 1"
elif [ $addr1 -ot $addr2 ] ; then
    echo "File 1 is older than file 2"
fi
[root@youxi1 ~]# touch file1 file2
[root@youxi1 ~]# stat file1
  File:"file1"
  Size: 0               Block: 0          IO Block: 4096 normal empty file
 Equipment: fd00h/64768d      Inode: 33574997    Hard link: 1
 jurisdiction:(0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Recent visit: 2019-10-12 15:17:18.341481971 +0800
 Recent changes: 2019-10-12 15:17:18.341481971 +0800
 Recent changes: 2019-10-12 15:17:18.341481971 +0800
 Created on:-
[root@youxi1 ~]# stat file2
  File:"file2"
  Size: 0               Block: 0          IO Block: 4096 normal empty file
 Equipment: fd00h/64768d      Inode: 33574998    Hard link: 1
 jurisdiction:(0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Recent visit: 2019-10-12 15:17:18.341481971 +0800
 Recent changes: 2019-10-12 15:17:18.341481971 +0800
 Recent changes: 2019-10-12 15:17:18.341481971 +0800
 Created on:-
[root@youxi1 ~]# cat file2
[root@youxi1 ~]# stat file2
  File:"file2"
  Size: 0               Block: 0          IO Block: 4096 normal empty file
 Equipment: fd00h/64768d      Inode: 33574998    Hard link: 1
 jurisdiction:(0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Recent visit: 2019-10-12 15:32:06.337458140 +0800
 Recent changes: 2019-10-12 15:17:18.341481971 +0800
 Recent changes: 2019-10-12 15:17:18.341481971 +0800
 Created on:-
[root@youxi1 ~]# sh a.sh / / the certificate has nothing to do with the access time atime
 Please enter the first address:/root/file1
 Please enter a second address:/root/file2
[root@youxi1 ~]# echo bbb >> file2
[root@youxi1 ~]# stat file2      
  File:"file2"
  Size: 4               Block: 8          IO Block: 4096 normal files
 Equipment: fd00h/64768d      Inode: 33574998    Hard link: 1
 jurisdiction:(0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Recent visit: 2019-10-12 15:32:06.337458140 +0800
 Recent changes: 2019-10-12 15:32:59.498600870 +0800
 Recent changes: 2019-10-12 15:32:59.498600870 +0800
 Created on:-
[root@youxi1 ~]# sh a.sh / / should be related to one of the change time mtime or ctime
 Please enter the first address:/root/file1
 Please enter a second address:/root/file2
 File 1 is older than file 2

Tags: shell

Posted by Smeep on Sun, 15 May 2022 08:44:35 +0300