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

Shell test statement-and or not

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

[root@master1-192,168117-18] # [- d / etc/dev] there should be a space on both sides of the test statement

[root@master1-192,168117-18] # echo $?

one

[root@master1-192,168117-18] # [- e / etc/profile]

[root@master1-192,168117-18] # echo $?

0

[root@master1-192,168117-18] # [- e / etc/profile] & & echo "I love you"

I love you

In addition to logical "and", there is also logical "OR". Its operation symbol in the Linux system is | |, which means that the commands following it will not be executed until the previous command fails, so it can be used to determine whether the currently logged-in user is a non-administrator combined with the system environment variable USER: [xiha@master1-192168117-18] $[$USER = root] | | echo "No's root".

No's root

The third logical statement is "not", and the operation symbol in the Linux system is an exclamation point (! Which means to take the opposite value of the judgment result in the conditional test That is, if the result of the original test is correct, it becomes wrong; the result of the original test error makes it correct.

[root@master1-192,168117-18] # [$USER! = root] | | echo "no's root"

No's root

First determine whether the USER variable name of the currently logged-in user is equal to root, and then use the logical operator "not" to reverse the operation, and the effect becomes to determine whether the currently logged-in user is a non-administrator user. Finally, if the condition is true, the word user will be output according to the logical "and" operator; or if the condition is not satisfied, the word root will be output through the logical "or" operator, and if the preceding & & is not true, the following | symbol will be executed.

[root@master1-192,168117-18] # [$USER! = root] & & echo "user" | | echo "root"

Root

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.

Share To

Servers

Wechat

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

12
Report