In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand linux shell logical operators and logical expressions". In daily operation, I believe many people have doubts about how to understand linux shell logical operators and logical expressions. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand linux shell logical operators and logical expressions". Next, please follow the editor to study!
I. logical operators
The logical volume label means 1. About the file and directory detection logic volume label! -f commonly used! Detect the existence of eg: if [- f filename]-d commonly used! Detect whether the "directory" exists-b detect whether it is a "block file"-c detect whether it is a "character file"-S detect whether it is a "socket tag file"-L detect whether it is a "symbolic link file"-e detect whether "something" exists! two。 About the logical volume label of the program! -G detects whether it is owned by a program executed by GID-O detects whether it is owned by a program executed by UID-p detects whether it is a name pipe or FIFO that sends information between programs (to be honest, I don't quite understand! ) 3. About the file attribute detection! -r detect whether it is a readable attribute-w detect whether it is a writable attribute-x detect whether it is an executable attribute-s detect whether it is a "non-blank file"-u detect whether it has the attribute of "SUID"-g detect whether it has the attribute of "SGID"-k detect whether it has the attribute of "sticky bit" 4. Judgment and comparison between two files; for example, [test file1-nt file2]-nt the first file is newer than the second file-ot the first file is older than the second file-ef the first file and the second file are the same file (link and the like) 5. Logical "and (and)" or (or) & & the meaning of logical AND | | the meaning of logical OR operation symbol = equals to be applied to: integer or string comparison if in [], it can only be a string! = does not mean to apply to: integer or string comparison if in [], the string can only be greater than in: integer comparison in [] Cannot use the representation string-eq equal to apply to: integer comparison-ne is not equal to apply to: integer comparison-lt less than applied to: integer comparison-gt greater than applied to: integer comparison-le is less than or equal to apply to: integer comparison-ge greater than or equal to apply to: integer comparison-a both sides hold (and) logical expression-a logical expression- O or logical expression-o logical expression-z empty string-n non-empty string
II. Logical expressions
Test command
Usage: test EXPRESSION
Such as:
The code is as follows:
[root@localhost] # test 1 = 1 & & echo 'ok'
Ok
[root@localhost] # test-d / etc/ & & echo 'ok'
Ok
[root@localhost] # test 1-eq 1 & & echo 'ok'
Ok
[root@localhost ~] # if test 1 = 1; then echo 'ok'; fi
Ok
Note: all characters and logical operators are directly separated by spaces and cannot be concatenated.
Reduced expression
The code is as follows:
[] expression
[root@localhost] # [1-eq 1] & & echo 'ok'
Ok
[root@localhost ~] # [2
< 1 ] && echo 'ok' -bash: 2: No such file or directory [root@localhost ~]# [ 2 \< 1 ] && echo 'ok' [root@localhost ~]# [ 2 -gt 1 -a 3 -lt 4 ] && echo 'ok' ok [root@localhost ~]# [ 2 -gt 1 && 3 -lt 4 ] && echo 'ok' -bash: [: missing `]' 注意:在[] 表达式中,常见的>, 5]] & & echo 'ok'
Ok
Note: the [[]] operator is just an extension of the [] operator. Can support symbolic operations do not need escape characters, it still compares the size of the string. The logical operator is supported: | | & &
III. Performance comparison
There are three almost equivalent symbols and commands in bash's conditional expression: test, [] and [[]]. Usually, people are accustomed to using the form if []; then. The emergence of [[]], according to ABS, is for compatibility >
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.