In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of conditional expression in Shell script, which is very detailed and has certain reference value. Friends who are interested must finish it!
I. document operation
1. Expression
-d: whether it is a directory
-f: whether it is a file or not
-e: whether a file or directory exists
-r: whether the current user has read permission
-w: whether the current user has write permissions
-x: whether the current user has executable permissions
2. Give an example (using the test command or [] in the command exercise)
[root@redis-singleton ~] # test-d / home/oldcat/tools/ [root@redis-singleton ~] # echo $? 0 [root@redis-singleton ~] # test-d / home/oldcat/tools/redis-3.0.7.tar.gz [root@redis-singleton ~] # echo $? 1 [root@redis-singleton ~] # test-f / home/oldcat/tools/redis-3.0.7.tar.gz [root@redis-singleton ~] # echo $? 0 description If the output execution result is 0, it is true On the contrary, it is false
2. Numerical comparison
1. Expression
-eq: whether the two operands are equal
-ne: whether the two operands are not equal
-le: whether the left Operand is less than or equal to the right Operand
-ge: whether the left Operand is greater than or equal to the right Operand
-lt: whether the left Operand is less than the right Operand
-gt: whether the left Operand is greater than the right Operand
2. Give an example
[root@redis-singleton ~] # test 1-eq 2 [root@redis-singleton ~] # echo $? 1 [root@redis-singleton ~] # test 1-eq 1 [root@redis-singleton ~] # echo $? 0 [root@redis-singleton ~] # test 2-gt 1 [root@redis-singleton ~] # echo $? 0 [root@redis-singleton ~] # test 1-ge 1 [root@redis-singleton ~] # echo $? 0 indicates that the output execution result is true, otherwise it is false
III. String comparison
1. Expression
=: whether two strings are equal
! =: whether two field strings are not equal
-z: determine whether the field string is empty
2. Give an example
[root@redis-singleton ~] # test "chenfl" = "chenfL" [root@redis-singleton ~] # echo $? 1 [root@redis-singleton ~] # test "chenfl"! = "chenfl" [root@redis-singleton ~] # echo $? 1 [root@redis-singleton ~] # test "chenfl" = "chenfl" [root@redis-singleton] # echo $? 0 [root@redis-singleton ~] # test-z "" [root@redis-singleton ~] # echo $? 0 [ Root@redis-singleton ~] # test-z "chenfl" [root@redis-singleton ~] # echo $? 1 description: true if the output execution result is 0 On the contrary, it is false
IV. Logical judgment
1. Expression
Logic and, only if both expressions are true (0), otherwise it is false (1).
| |: logical OR. As long as one of the two expressions is true, the execution result is true (0), otherwise it is false (1) |
!: logically non, false if the execution result of the expression is true (1), and true if the execution result of the expression is false (0)
2. Give an example
[root@redis-singleton ~] # [1-eq 1] & & ["hehe"! = "hehe"] [root@redis-singleton ~] # echo $? 1 [root@redis-singleton ~] # [1-eq 1] | | ["hehe"! = "hehe"] [root@redis-singleton ~] # echo $? 0 [root@redis-singleton ~] # [!-d / home/oldcat/tools/] [root@redis-singleton ~] # echo $? 1 description: true if the output execution result is 0 Otherwise, the above is all the content of the article "sample Analysis of conditional expressions in Shell script". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.