Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of conditional expression in Shell script

Shulou Source: shulou.com Published: 2022-06-03 02:22:52 09月21日 Update

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!

Tags: Expressions operands results two logic output files permissions users examples scripts middle bars analysis content commands fields characters strings directories articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information MariaDB Apple Xiaomi