In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to understand the use of square brackets in Shell. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Introduction in computer science, Shell, commonly known as shell (used to distinguish it from kernel), refers to software (command parser) that "provides the user with an interface". It is similar to command.com under DOS and later cmd.exe. It receives user commands and then invokes the appropriate application.
The square brackets in Shell (including single and double brackets) can be used to test some conditions:
Arithmetic comparison, such as whether a variable is 0, [$var-eq 0].
File properties test, such as whether a file exists, [- e $var], whether it is a directory, [- d $var].
String comparison, such as whether two strings are the same, [[$var1 = $var2]].
[] you can often use the test command instead, which is described later.
Arithmetic comparison
To make an arithmetic conditional judgment on a variable or value:
[$var-eq 0] # returns true [$var-ne 0] when $var equals 0 # returns true when $var is not equal to 0
It should be noted that there must be a space between [and] and the Operand, otherwise an error will be reported. For example, the error will be reported as follows:
[$var-eq 0] or [$var-ne 0]
Other comparison operators:
Operator meaning-gt greater than-lt less than-ge greater than or equal to-le less than or equal to
You can test with-a (and) or-o (or) with multiple conditions:
[$var1-ne 0-a $var2-gt 2] # use logic and-a [$var1-ne 0-o $var2-gt 2] # use logic or-o
File system property test
Use different conditional flags to test different file system attributes.
Operator meaning [- f $file_var] variable $file_var is a normal file path or file name (file), then return true [- x $var] variable $var contains file executable (execute), return true [- d $var] variable $var contains file is directory (directory), return true [- e $var] variable $var contains file existence (exist) The true [- c $var] variable $var contains the path (character) of a character device file, the true [- b $var] variable $var contains the path (block) of a block device file, the true [- w $var] variable $var contains a writable file (write), and the true [- r $var] variable $var contains a file readable (read). Returns true [- L $var] variable $var contains a symbolic link (link), returns true
The method of use is as follows:
Fpath= "/ etc/passwd" if [- e $fpath]; then echo File exits;else echo Does not exit;fi
String comparison
When comparing strings, it is best to use double parentheses [[]]. Because single parentheses can cause some errors, it's best to avoid them.
Check whether the two strings are the same:
[[$str1 = $str2]]
Returns true when str1 equals str1 equals str2. That is, str1 and str2 contain the same text. The single equals sign can also be written as a double equals sign, that is, the above string comparison is equivalent to [$str1 = = $str2].
Notice that there is a space before and after =. If you forget to add a space, it becomes an assignment statement, not a comparative relationship.
Other comparisons of strings:
Operator meaning [[$str1! = $str2]] if str1 is not the same as str2, return true [- z $str1]] if str1 is an empty string, return true [- n $str1]] if str1 is a non-empty string, return true
Use the logical operators & & and | | to easily combine multiple conditions, such as:
Str1= "Not empty" str2= "if [[- n $str1]] & & [[- z $str2]]; then echo str1 is nonempty and str2 is empty string.fi
The test command can also never perform conditional checks, using test can avoid using too many parentheses, and the test conditions in [] can also be done through test.
If [$var-eq 0]; then echo "True"; fi
Equivalent to:
If test $var-eq 0; then echo "True"; the above is how to understand the use of square brackets in Shell. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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
# 1.57## [PROD #] telnet 109.120.1.86 7575Trying...Connected to 109.1
© 2024 shulou.com SLNews company. All rights reserved.