In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
How to carry out conditional testing of Shell scripts and the use of if conditional statements? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Conditional test operation
The test command is a special testing tool that can test a specific condition and then determine whether the condition is true or not based on the return value.
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 $? 12. 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 ~] # bau2 [root@centos01 ~] # [$a! = $b] & & echo "yes" yes4, 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 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 Downkeeper statement 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 answer to the question on how to conduct conditional testing of Shell scripts and the use of if conditional statements. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.