Conditional statement of shell programming-- if and case
Conditional statements of shell programming-- if and case I. File test 1.1test command
Test whether a specific expression is valid, and the return value is 0, otherwise, non-0
Test conditional expression
[conditional expression]-- add a space between the content and parentheses
File testing
[operator file or directory]
Commonly used test operators
●-d: test whether it is a directory (Directory)
●-e: test whether a directory or file exists (Exist)
●-f: test whether it is a file (File)
●-r: test whether the current user has permission to read (Read)
●-w: test whether the current user has permission to write (Write)
●-x: test whether the current user has permission to execute (Excute)
Examples are as follows (beginners must knock on their own to experience and understand):
[root@localhost] # ls-l shell/ total dosage 16m r w r m r m el el. 1 root root 134. November 25 19:59 demo.sh-rwxr-xr-x. 1 root root 274 November 25 18:58 state.sh-rwxr-xr-x. 1 root root 208 November 25 19:17 sujiaju.sh-rwxr-xr-x. 1 root root 345 November 25 19:11 welcome.sh [root@localhost ~] # [- d shell/] / / the first test method is valid for current users, or test-d shell/ [root@localhost ~] # echo $? 0 [root@localhost ~] # [- d shell/state.sh] [root@localhost ~] # echo $? 1 [root@localhost ~] # test-d shell/ the second test method Valid for the current user [root@localhost ~] # echo $? 0 [root@localhost ~] # test-d shell/welcome.sh [root@localhost ~] # echo $? 1 [root@localhost ~] # test-x shell/welcome.sh [root@localhost ~] # echo $? 0 [root@localhost ~] # test-x shell/demo.sh [root@localhost ~] # echo $? 1Univer / the above process can be used & once execute [root@localhost ~] # test -x shell/welcome.sh & & echo "yes" yes [root@localhost ~] # echo $? 0 [root@localhost ~] # [- d shell/state.sh] & & echo "yes" [root@localhost ~] # echo $? 1
The test command can also be used to determine the relationship between files:
-nt-- determine whether A file is newer than B file
-ot-- determines whether file An is older than file B.
-ef-- determines whether two files are the same file (whether the inode is the same or not) 1.2 integer value comparison
[integer 1 operator integer 2]
Test operators commonly used in ■
●-eq: equal to (Equal)
●-ne: not equal to (Not Equal)
●-gt: greater than (Greater Than)
●-lt: less than (Lesser Than)
●-le: less or equal to (Lesser or Equal)
●-ge: greater than or equal to (Greater or Equal)
Reference example:
[root@localhost shell] # [5-gt 3] & & echo "yes" yes [root@localhost shell] # [5-lt 3] & & echo "yes" [root@localhost shell] # 1.3 string comparison
● format 1
[string 1 = string 2]
[string 1! = string 2]
● format 2
[- Z string]
Test operators commonly used in ■
● =: the string content is the same
●! =: string content is different,! The sign means the opposite.
●-Z: the string content is empty
[root@localhost shell] # [hello = hello] & & echo "yes" yes [root@localhost shell] # [hello = hell] & & echo "yes" [root@localhost shell] # [hello! = hell] & & echo "yes" yes [root@localhost shell] # [- z] & & echo "yes" yes [root@localhost shell] # [- z hello] & & echo "yes" [root@localhost shell] # 1.4 Logic Test
format 1: [expression 1] operator [expression 2]
format 2: command 1 operator command 2.
Commonly used test operators
●-an or & &: logic and, 'and'
●-o or | |: logical OR, meaning "or"
●!: logical No
The instance can refer to the string test instance above.
5. The structure of if sentence 5.1 single branch structure:
If conditional test operation
then command sequence
Fi
5.2 double branch structure
If conditional test operation
then Command sequence 1
else Command sequence 2
Fi
5.3 Multi-branch structure
If conditional Test Action 1
then Command sequence 1
Elif conditional Test Action 2
then Command sequence 2
Else
Command sequence 3
Fi
6. Case sentence structure format: case variable value in mode 1) command sequence 1; pattern 2) command sequence 2; *) default command sequence esac summary: if condition judgment is generally used for interval judgment, case is generally used for fixed value or string judgment.