Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Conditional testing of Shell scripts and use of if conditional statements

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Blog catalogue

Conditional test operation

1. File testing:

2. Integer value comparison:

3. String comparison:

4. Logic test:

Second, use if conditional statements

1. Single branch if statement

2. Double-branch if statement

3. Multi-branch if statement

Conditional test operation

To make Shell scripts "intelligent", the first problem is how to distinguish between different situations to determine what to do. The Shell environment returns the status value (¥?) after the command is executed. To determine whether the execution was successful, if the return value is 0, it is successful, otherwise (non-0 value) indicates failure or exception Using a special testing tool, the test command, you can test a specific condition and determine whether the condition is true according to the return value (a return value of 0 indicates that the condition is true).

When using the test test command, it includes the following two forms:

Test conditional expression

These two ways work exactly the same, but the latter form is usually more common, and it is important to note that at least one space is needed to separate the square bracket "[" or "]" from the conditional expression.

1. File testing:

-d: test whether it is a directory (Directory)

-e: test whether a file or directory 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 executable (Excute) permission is set

After performing the conditional test operation, through the predefined variable $? The return status value of the test command can be obtained to determine whether the condition is true. For example, do the following to test whether the directory / media/ exists, if a value of $is returned? 0 indicates that this directory exists, otherwise it does not exist or is not a directory although it exists.

[root@centos01 ~] # test-d / boot [root@centos01 ~] # echo $? 0

If the test condition is not valid, the return value of the test operation will not be 0 (usually 1).

[root@centos01 ~] # test-d / ertec [root@centos01 ~] # echo $? 1 2. Integer value comparison:

-eq: the first number equals (Equal) the second number

-ne: the first number is not equal to (Not Equal) the second number

-gt: the first number is greater than (Greater Than) the second number

-lt: the first number is less than (Lesser Than) the second number

-le: the first number is less than or equal to (Lesser or Equal) the second number

-ge: the first number is greater than or equal to (Greater or Equal) the second number

Integer value comparison is widely used in Shell scripting. For example, it is used to determine the number of logged-in users, the number of open processes, whether the disk utilization exceeds the standard, and whether the software version number is symbolically required. In actual use, a numerical value is often obtained by means of variable reference, command replacement and so on.

3. String comparison:

The first string is the same as the second string.

! =: the first string is different from the second string, where "!" Denotes rebellion.

-z: checks whether the string is empty, and will be an empty string for variables that are not defined or assigned null values. [root@centos01 ~] # aqum1 [root@centos01 ~] # bread2 [root@centos01 ~] # [$a = $b] & & echo "yes" [root@centos01 ~] # axi1 [root@centos01 ~] # root@centos01 ~] # [$a! = $b] & & echo "yes" yes 4, logic test:

The & &: logic and means "and". The return value of the entire test command is 0 (the result is valid) only if both conditions are true. When testing with the test command, "&" can be replaced with "- a".

| |: logical OR, which means "or". As long as one of the two conditions is true, the value returned by the whole test command is 0 (the result is valid). You can use "- o" instead when testing with the test command.

! Logical no means "no". Only when the condition is not valid, the value returned by the entire test command is 0 (the result is valid).

Examples are as follows:

[root@centos01 ~] # yes 100 [$aq100] & & [$a! = 50] & & echo "yes" yes [root@centos01 ~] # root@centos01 ~] # [$aq100] | | [$a-ge 50] & & echo "yes" yes II, use if conditional statement 1, single branch if statement

The "branch" of the if statement refers to the execution statement (one or more) corresponding to different test results. For the selection structure of a single branch, the corresponding code is executed only when the condition is established, otherwise no action is performed. The syntax format of the single-branch if statement is as follows:

Example of a single branch if statement:

[root@centos01 ~] # cd / usr/src/ppp-bash: cd: / usr/src/ppp: there is no such file or directory [root@centos01 ~] # vim if_dan.sh #! / bin/bash mount= "/ usr/src/ppp" if [!-d $mount] then mkdir-p $mount fi [root@centos01 ~] # chmod + x if_dan.sh [root@centos01 ~] #. / if_dan.sh [root@centos01 ~] # cd / usr/src/ppp/ [root@centos01 ppp] # ls2, Double branch if statement

For the selection structure of two branches, different operations should be performed according to the two cases of "condition is established" and "condition is not". The syntax format of the two-branch if statement is as follows:

Example of a two-branch if statement:

[root@centos01 ~] # vim if_shuang.sh #! / bin/bashping-c 3-I 0.2-W 3 $1 & > / dev/null if [$?-eq 0] then echo "Host:$1 is downloads!" else echo "Host:$1 is downloads!" fi [root@centos01 ~] # chmod + x if_shuang.sh [root@centos01 ~] # . / if_shuang.sh 192.168.100.10 Host:192.168.100.10 is UP!!! [root@centos01 ~] #. / if_shuang.sh 192.168.100.103 Host:192.168.100.103 is download statements! 3. Multi-branch if statement

Because the if statement can be executed separately according to the test results, all of them can be used in nesting and make multiple judgments. For example, first of all, judge whether a student's score is passed, if he passes, then judge whether it is higher than 90 points and so on. The syntax format of the multi-branch if statement is as follows:

Example of a multi-branch if statement:

[root@centos01] # vim if_duo.sh #! / bin/bashread-p "Please enter the test result:" insert if [$insert-ge 85] & & [$insert-le 100] then echo "Congratulations on your excellent exam results!" Elif [$insert-ge 70] & & [$insert-le 84] then echo "Congratulations on passing the exam!" Else echo "it's a pity that you can pack up your exam results and plant rice at home!" Fi [root@centos01 ~] # chmod + x if_duo.sh [root@centos01 ~] #. / if_duo.sh Please enter the test result: 94 Congratulations on your excellent exam result! [root@centos01 ~] # / if_duo.sh Please enter the exam score: 82 Congratulations on your passing the exam! [root@centos01 ~] # / if_duo.sh Please enter the test score: 71 Congratulations on your passing the exam! [root@centos01 ~] # / if_duo.sh Please enter the test score: 32 I'm sorry you can pack up and plant rice at home!

-this is the end of this article. Thank you for reading-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 273

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report