Conditional judgment of Shell script

Keywords: Linux shell bash

1. Basic grammar

[condition] (note that there should be spaces before and after the condition) Note: if the condition is not empty, it is true, [666] returns true and [] returns false.

2. Common judgment conditions

(1) Comparison between two integers = string comparison
-lt less than
-le less than or equal
-eq equals (equal)
-gt greater than
-ge is greater than or equal
-ne is Not equal to (Not equal)
(2) Judge according to file permissions
-r has read permission (read)
-w has write permission (write)
-x has execute permission (execute)
(3) Judge by file type
-The f file exists and is a regular file
-e file exists
-d file exists and is a directory

Note: if the execution status of any command is 0, it is True, otherwise it is False

3. Case

a. Judge whether a file exists in the current directory
[- e filename]
echo $?
b. Determine whether the current file exists and is a directory
[- d filename]
echo $?

[root@mylinux2 shell]# [ 23 -lt 24 ]
[root@mylinux2 shell]# echo $?
0
[root@mylinux2 shell]# [ 23 -le 24]
-bash: [: lack `]'
[root@mylinux2 shell]# [ 23 -le 24 ]
[root@mylinux2 shell]# echo $?
0
[root@mylinux2 shell]# [ 23 -le 22 ]
[root@mylinux2 shell]# echo $?
1

04_Special variable-1.sh Do you have write permission
[root@mylinux2 shell]# [- w 04_; special variable - 1.sh]
[root@mylinux2 shell]# echo $?
0

04_Special variable-1.sh Do you have executable permissions
[root@mylinux2 shell]# [- x 04_; special variable - 1.sh]
[root@mylinux2 shell]# echo $?
1

04_Special variable-1.sh Is it a regular file( file)
[root@mylinux2 shell]# [- f 04_; special variable - 1.sh]
[root@mylinux2 shell]# echo $?
0

04_Special variable-1.sh Is it a directory
[root@mylinux2 shell]# [- d 04_; special variable - 1.sh]
[root@mylinux2 shell]# echo $?
1

Judgment document 04_Special variable-1.sh Does it exist in the current directory
[root@mylinux2 shell]# [- e 04_; special variable - 1.sh]
[root@mylinux2 shell]# echo $?
0


(4) Multi condition judgment
&&Indicates that the last command is executed only when the previous command is executed successfully;
||Indicates that the next command is executed after the execution of the previous command fails

Logic and
a

[root@mylinux2 shell]# [ 23 -lt 24 ] && echo ok
ok

Why print ok?
Because 23 < 24 in the conditional expression is True, and True and another condition cannot determine the whole result, the echo ok after & & should be executed

b,

[root@mylinux2 shell]# [ 25 -lt 24 ] && echo ok
[root@mylinux2 shell]# echo $?
1

Why is it not performed correctly?
Because the conditional expression 25 < 24 is False, and False & & XXX, XXX is False, any logical operation is False, so the following echo ok does not need to be executed

Logical or

[root@mylinux2 shell]# [ 25 -lt 24 ] || echo ok
ok

Why print ok?
Because 25 < 24 in the conditional expression is False, and False and another condition cannot determine the whole result, the following echo ok should be executed

[root@mylinux2 shell]# [ 23 -lt 24 ] || echo ok
[root@mylinux2 shell]# echo $?
0

Why is it performed correctly?
Because the conditional expression 23 < 24 is True, True | XXX, XXX is True regardless of any logical operation, so the output is 0

4. if judgment

1. Basic grammar

if [ Conditional judgment ];then   
	program 
fi  

matters needing attention:
1) [conditional judgment], there must be a space between brackets and conditional judgment
2) There should be a space after if

The script is as follows:

Execution code:

[root@mylinux2 shell]# bash 09if judgment -1.sh 12
 under age
[root@mylinux2 shell]# bash 09if judgment -1.sh 24
 youth
[root@mylinux2 shell]# bash 09if judgment -1.sh 33
 Middle aged and elderly

Posted by phpIsKillingMe on Sun, 05 Dec 2021 18:34:49 -0800