In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article gives you a detailed introduction to the syntax structure and usage of conditional judgment statements in Shell scripts. Most of the commands and knowledge points are often used by everyone, so we can share them for your reference. Let's follow the editor and have a look.
The grammatical structure of conditional judgment
Think: what is true? What is false?
# # 1. Conditional judgment grammar format
Format 1: test conditional expression format 2: [conditional expression] format 3: [[conditional expression]] supports regular
Special note:
1) [imp:2, I have spaces on both sides, I don't have time to kill you]: [kiss, I have spaces on both sides, if I don't have time to kill you]]: imp:3) more judgments, 'man test` to check, a lot of parameters are used for conditional judgment 2. Condition judgment related parameters 1) judge file type judge parameter meaning-e determine whether the file exists (any type of file)-f determine whether the file exists and is an ordinary file-d determine whether the file exists and is a directory-L determine whether the file exists and is a soft connection file-b determine whether the file exists and is a block device file-S judgment Whether the file exists and is a socket file-c determines whether the file exists and is a character device file-p determines whether the file exists and is a named pipe file-s determines whether the file exists and is a non-empty file (with content)
Examples are as follows:
Test-e file as long as the existence condition of the file is true [- d / shell01/dir1] to determine whether the directory exists, the existence condition is true [!-d / shell01/dir1] to determine whether the directory exists, and the non-existence condition is true [[- f / shell01/1.sh]] to determine whether the file exists. And it is an ordinary file 2) to judge whether the file permission is readable by the current user-w whether the current user is writable to it-x whether the current user is executable-u has suid, whether the advanced permission risk bit-g is sgid, whether the advanced permission enforcement bit-k has t bits, and whether the advanced permission stickiness bit 3) judge whether the file is new or old
Description: the new and old here refers to the modification time of the file.
Judge parameter meaning file1-nt file2 compare file1 to file2 new file1-ot file2 compare file1 to old file2 file1-ef file2 whether it is the same file, or used to determine hard connection, whether it points to the same inode4) judge integer judgment parameter meaning-eq equal-ne unequal-gt greater than-lt less than-ge greater than equal-le less than equal 5) judge string judgment parameter meaning-z determine whether it is an empty string If the string length is 0, it holds-n to determine whether it is a non-empty string. If the string length is not 0, string1 = string2 to judge whether the string is equal string1! = string2 to judge whether the string is not equal 6) multiple conditional judgment examples of symbolic meaning-an and & & logic and [1-eq 1] & & [1-ne 0]-o and | | logic or [1-eq 1-o 1-ne 1]
Special note:
The following code will not be executed until the previous expression is true
| the following code will be executed only if the previous expression is false |
; used only for split commands or expressions
① illustrates numerical comparison [root@server ~] # [$(id-u)-eq 0] & & echo "the user is admin" [root@server ~] $[$(id-u)-ne 0] & & echo "the user is not admin" root@server ~] $[$(id-u)-eq 0] & & echo "the user is admin" | echo "the user is not admin" [root@server ~] # uid= `id-u` [root] @ server ~] # test $uid-eq 0 & & echo this is adminthis is admin [root@server ~] # [$(id-u)-ne 0] | | echo this is adminthis is admin [root@server ~] # [$(id-u)-eq 0] & & echo this is admin | | echo this is not adminthis is admin [root@server ~] # su-stu1 [stu1@server ~] $[$(id-u)-eq 0] & echo this is admin | echo this Is not adminthis is not admin class C style numerical comparison note: in () = indicates assignment = = to indicate judgment [root@server ~] # ((1qq2)); echo $? [root@server ~] # ((1x1)); echo $? [root@server ~] # ((2roomroom1)); echo $? [root@server ~] # ((`id-u` = = 0)); echo $? [root@server ~] # ((aq123)); echo $a [root@server ~] # unset a [root@server ~] # ((Awarz123)); echo $? String comparison note: double quotation marks are quoted as a whole Both = and = = in [string] comparison indicate judgment [root@server ~] # a='hello world';b=world [root@server ~] # [$a = $b]; echo $? [root@server ~] # ["$a" = "$b"]; echo $? [root@server ~] # ["$a"! = "$b"]; echo $? [root@server ~] # ["$a"! = "$b"]; echo $? Error [root@server ~] # ["$a" = "$b"]; echo $? [root@server ~] # test "$a"! = "$b"; echo $? test expression [expression] [[expression]] what's the difference between [] and [[]]? [root@server ~] # a = [root@server ~] # test-z $a X echo $? [root@server ~] # a=hello [root@server ~] # test-z $a X echo $? [root@server ~] # test-n $a X echo $? [root@server ~] # test-n "$a"; echo $? # ['= a]; echo $?-bash: [: unary operator expected2# [['= $a]] Echo $? 0 [root@server ~] # [1-eq 0-a 1-ne 0]; echo $? [root@server ~] # [1-eq 0 & & 1-ne 0]; echo $? [root@server ~] # [1-eq 0 & & 1-ne 0]; echo $? logical operator summary 1. Symbols; and & & and | | can be used to split commands or expressions 2. The semicolon (;) will execute regardless of whether the previous statement is executed correctly; the content after the semicolon 3. `& &` symbol, need to consider the correctness of the previous statement, the previous statement will be executed correctly, and vice versa 4. `|` symbol, need to consider | | the previous statement is incorrect, the previous statement execution error will be executed | | latter content, and vice versa 5. If & & and | | appear together, look from left to right in turn, according to the above principles
The above is the introduction of the syntax structure and usage of conditional judgment statements in Shell scripts. Try the use of conditional statements yourself. More content is 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.