In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is about what operators are in Shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Example of arithmetic operator description + addition `expr $a + $b`-subtraction `expr $a-$b` * multiplication `expr $a\ * $b` / division `expr $b% $a` = assign the value of variable b to a. = = equal. It is used to compare two numbers, and true is returned if the same number is the same. [$a = = $b] returns false. ! = not equal. It is used to compare two digits. True is returned if they are different. [$a! = $b] returns true
Example:
#! / bin/bash#author:zrca=5b=10val= `expr ${a} + ${b} `expr "a + b: ${val}" val= `expr ${a}-${b} `expr "a-b: ${val}" val= `expr ${a}\ * ${b} `echo "a * b: ${val}" val= `expr ${a} / ${b} `expr "a / b: ${val}" # pay attention to the use of if [$a = = $b]; then echo "a = b" fiif [$a! = $b] Then echo "a! = b" fi relational operator
The relational operator does not support strings (see below for strings), but only supports numeric examples: axi5biz10
Operator description example-eq detects whether two numbers are equal, and equality returns true. [$a-eq $b] returns false. -ne checks whether two numbers are not equal, and returns true. [$a-ne $b] returns true. -gt detects whether the number on the left is greater than that on the right, and if so, returns true. [$a-gt $b] returns false. -lt detects whether the number on the left is smaller than that on the right, and if so, returns true. [$a-lt $b] returns true. -ge detects whether the number on the left is greater than or equal to the number on the right, and if so, returns true. [$a-ge $b] returns false. -le detects whether the number on the left is less than or equal to the number on the right, and if so, returns true. [$a-le $b] returns true.
Example:
#! / bin/bash#author:zrca=5b=10if [$a-eq $b]; then echo "$a-eq $b: an equals b" else echo "$a-eq $b: an is not equal to b" fiif [$a-ne $b]; then echo "a-ne $b: an is not equal to b" else echo "$a-ne $b: an equals b" fi Boolean operator
Example: axiom 5 bu 10
Operator illustration example! If the expression is true, it returns false, otherwise it returns true. [! False] returns true. -o or operation. If an expression is true, true is returned. [$a-lt 20-o $b-gt 100] returns true. -an and operation, both expressions are true before returning true. [$a-lt 20-a $b-gt 100] returns false. Logical operator
Note that the use of logical operators requires multiple use of [] symbols example: axiom 5 bu 10
True and flase returns false
True and false returns true
Operator description example & logical AND [[$a-lt & & $b-gt 100]] returns false | | logical OR [[$a-lt 100 | | $b-gt 100]] returns the true string operator
The following table lists the common string operators, assuming that the variable an is "aaa" and the variable b is "bbb":
Operator description example = checks whether two strings are equal, and equality returns true. [$a = $b] returns false. ! = checks whether two strings are equal, and returns true if not. [$a! = $b] returns true. -z detects whether the string length is 0, and returns true if it is 0. [- z $a] returns false. -n detects whether the string length is 0, and returns true if it is not 0. [- n "$a"] returns true. $checks whether the string is empty, and returns true if it is not empty. [$a] returns true.
Example:
#! / bin/bash#author:zrca= "aaa" b = "bbb" if [$a = $b]; then echo "$a = $b: an equals b" else echo "$a = $b: an is not equal to b" fiif [$a! = $b]; then echo "$a! = $b: an is not equal to b" else echo "$a! = $b: an equals b" fiif [- z $b] Then echo "- z $b: character length is 0" else echo "- z $b: character length is ${# b}" fiif [- n $b]; then echo "- z $b: character length is not 0" else echo "- z $b: character length is ${# b}" fiif [$b] Then echo "$b: string is not empty" else echo "$b: string is empty" fi file operator description example-b file detects whether the file is a block device file, and if so, returns true. [- b $file] returns false. -c file detects whether the file is a character device file, and if so, returns true. [- c $file] returns false. -d file detects whether the file is a directory, and if so, returns true. [- d $file] returns false. -f file detects whether the file is a normal file (neither a directory nor a device file), and if so, returns true. [- f $file] returns true. -g file detects whether the file has the SGID bit set, and if so, returns true. [- g $file] returns false. -k file detects whether the file has an adhesion bit (Sticky Bit) set, and if so, returns true. [- k $file] returns false. -p file detects whether the file is a named pipe, and if so, returns true. [- p $file] returns false. -u file detects whether the file has the SUID bit set, and if so, returns true. [- u $file] returns false. -r file detects whether the file is readable, and if so, returns true. [- r $file] returns true. -w file detects whether the file is writable, and if so, returns true. [- w $file] returns true. -x file detects whether the file is executable, and if so, returns true. [- x $file] returns true. -s file detects whether the file is empty (whether the file size is greater than 0), and returns true if it is not empty. [- s $file] returns true. -e file detects the existence of files, including directories, and if so, returns true. [- e $file] returns true. Thank you for reading! This is the end of this article on "what are the operators in Shell?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.