In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The arithmetic operators are as follows:
Operation operators and operation commands:
1. Double parentheses "()" numerical operation command
The function of double parentheses "()" is to compare numerical operations with numerical values. It is the most efficient and flexible operator, and is often used as an operation operator.
When you execute the echo$ ((aqu +)) and echo$ ((Amura -)) commands to output the entire expression, the output value is the value of a. After the expression is executed, the operation of + +,-- will be carried out on a, while the echo$ ((+ + a)) and echo$ ((--a) commands will output the entire expression first with + +,--, and then output the value of the expression, that is, the value after the an operation.
Note: memory methods for + + and-- operations:
Variable a before the operator, the value of the output expression is a, and then an increases or decreases; after the operator, the output expression increases or decreases, and the value of the expression is the value of an after self-increment or self-subtraction. " (()) "when the expression is executed on the command line, you don't need to add the $sign, just use the ((6% 2)) form, but if you need output, add the $symbol, for example: echo$ ((6% 2))." The absence of spaces and one or more spaces between all characters in (()) "will not affect the result.
Examples of use:
[root@localhost ~] # echo $((6x2)) # addition 8 [root@localhost ~] # echo $((6-2)) # subtraction 4 [root@localhost ~] # echo $((6x2)) # multiplication 12 [root@localhost ~] # echo $((6x2)) # Division 3 [root@localhost ~] # echo $((6% 2)) # take the remaining 0 [root@localhost ~] # echo $((6% 2)) * * 2)) # Square 36 [root@localhost ~] # aq3 # assign a value to the variable [root@localhost ~] # echo $((aquifer +)) # output the variable value first Then + 13 [root@localhost ~] # echo ${a} # look at the variable, and find that + 1 has 4 [root@localhost ~] # echo $((Amuri -)) # first output the variable value, and then-14 [root@localhost ~] # echo ${a} # look at the variable, and find that-1 has 3 [root@localhost ~] # echo $((+ + a)) # first + 1, then output the variable value Output is the value after + 1 4 [root@localhost ~] # echo ${a} # confirm variable value 4 [root@localhost ~] # echo $((--a)) # first-1, and then output variable value 3 [root@localhost ~] # echo ${a} # to confirm the use of 32, let operation commands
The function of let assignment expression is equivalent to "((assignment expression))"
Examples of use:
[root@localhost ~] # let i=i-1 1 [root@localhost ~] # echo $i1 [root@localhost ~] # let i=i+8 # plus 8 [root@localhost ~] # echo $I # output 9 [root@localhost ~] # let i=i-1 # iLizhi 1 [root@localhost ~] # echo $I # output 8 [root@localhost ~] # let iTunes + # I often use this method in shell script Default + 1 [root@localhost ~] # echo $i9 [root@localhost ~] # let imuri-# iMuth1 [root@localhost ~] # echo $i83, usage of the expr command
Function:
The expr (evaluate (evaluation) expressions (expression)) command can be used not only for integer operations, but also for related string length, matching, and so on.
(1) expr is used to calculate:
[root@localhost ~] # expr 2 + 35 [root@localhost ~] # expr 2\ * 3 # * after escaping, use 6 [root@localhost ~] # expr 6 / 32 [root@localhost ~] # expr 7% 31
Note when using expr:
The operator and the number used for the calculation have at least one space around, otherwise an error will be reported. When using a multiplication sign, it must be escaped (converted to a normal character) with a backslash, because Shell may misunderstand the meaning of the asterisk.
(2) calculation of expr fit variables
[root@localhost ~] # item5 [root@localhost ~] # I = `expr ${I} + 5` # you need to enclose it with a backapostrophe. Note that there are spaces [root@localhost ~] # echo ${I} 10 on both sides of variables and numeric symbols.
(3) to judge whether a variable value or string is an integer.
The principle of implementation is that, using the rule that a variable or string must be an integer when calculated in expr, add a variable or string to a known integer (non-0) to see whether the value returned by the command is 0. If 0, the variable or string doing the addition is considered to be an integer, otherwise it is not an integer.
Example 1:
[root@localhost ~] # axi2 [root@localhost ~] # expr ${a} + 57 [root@localhost ~] # echo $? # output is 0, indicating that variable an is an integer 0 [root@localhost ~] # b=lvjianzhao # defining a character variable [root@localhost ~] # expr ${b} + 5 # actually directly reports an error expr: non-integer parameter [root@localhost ~] # echo $? # output is non-zero Then the variable b is not an integer 2
Example 2:
[root@localhost ~] # vim test.sh #! / bin/bashexpr $1 + 4 & > / dev/nullif [$?-eq 0] then echo int else echo charsfi [root@localhost ~] # sh test.sh aaa chars # output is the string [root@localhost ~] # sh test.sh 34int # output is an integer
Example 3:
#! / bin/bashtuichu () {exit 1} while truedo read-p "pls input:" an if [$a = = exit] then tuichu # call the function fi expr $a + 0 > / dev/null 2 > & 1 [$?-eq 0] & & echo int | | echo charsdone [root@localhost ~] # sh test. Sh # execute script pls input:22 # enter numbers to test intpls input:dfdf # enter characters to test charspls input:exit # enter exit The function will be triggered and the script will exit. If you do not define this function, you can only force the interrupt script to execute chars [root@localhost ~] # 4 through ctrl+c. The usage of bc computer
(1) enter bc directly in command mode, press enter and then use it directly, as follows:
[root@localhost ~] # bc # enter bc into the computer environment bc 1.06.95. # omit part of the content 1: 8 # enter and press enter to get the result 99p336. 212.
(2) use bc under the command line to achieve the operation function:
[root@localhost ~] # echo 3x4 | bc # output to bc computer 7 [root@localhost ~] # echo "3.4x5.6" | bc9.0 [root@localhost ~] # echo "3.22.5" | bc.7 [root@localhost ~] # echo "scale=2;355/113" | bc # use the scale parameter to set the reserved decimal place 3.14 [root@localhost ~] # echo "scale=6;355/113" | bc3.141592
(3) use bc to cooperate with variable operation:
[root@localhost ~] # iTun5 [root@localhost ~] # I = `echo $iTun6 | bc` uses echo output and gives it to bc through pipe characters. This method is inefficient and is generally not used. [root@localhost ~] # echo $i11
Tip: according to the particularity of bc, it is no problem to choose bc operation if it is a decimal number (it is more recommended to use awk); for integer scenarios, you can use "()", let, expr, etc.
(4) use a command to evaluate the expression outputting 1 "2" 3. 10, and evaluate the result (implemented by bc computer)
The ways to generate 1 "2" 3. 10 expressions are:
[root@localhost ~] # seq-s "+" 10 # seq is a sequence of generated digits, and-s is the separator between specified sequences of digits: 1 "2" 3 "4" 5 "6" 7 "8" 10 [root@localhost ~] # echo {1... 10} | tr "+" generates a sequence of numbers separated by spaces and gives tr to replace the spaces with the + sign 1 "2" 3 "4" 5 "7" 8 "9" 10
Several ways to implement the above requirements are as follows:
# use bc to calculate [root@localhost ~] # echo `seq-s "+" 10` = `seq-s "+" 10 | bc`1 + 2 "3" 4 "5" 6 "7" 8 "9" 10 5 "(()) use () calculate [root@localhost ~] # echo `seq-s" + "10` = $((`seq-s" + "10`)) 1" 2 "3" 4 "5" 6 "8" 9 "10" 5) use expr to calculate [root@localhost ~] # echo `seq-s'+ '10` = `seq-s "+ 10 | xargs expr`1 + 2" 3 "4" 5 "6" root@localhost ~] # echo seq-s' + '1055 use [] to calculate [root@localhost ~] # echo seq-s' + '1055 (echo $[`seq-s'+ '10`]) 1 "2" 3 "4" 5 "6" 7 "8" 9 "10" 555, Implementing Computing function with awk
The effect of using awk for operation is also very good, it is suitable for decimals and integers, especially for command line calculation, especially for decimals, which is accurate and easy to use.
Examples of use:
[root@localhost ~] # echo "7.78.5" | awk'{print ($1mura 2)}'- 0.8 [root@localhost ~] # echo "7.73.5" | awk'{print ($1mura 2)} '4.2 [root@localhost ~] # echo "358,114" | awk' {print ($1-2) / $2} '3.12281 [root@localhost ~] # echo "3" | usage of awk' {print ($1mm 3) * $2} '486, declare (same as typeset) command
You need to use typeset to define integer variables and calculate them directly, which is not commonly used because it needs to be defined to take effect.
Examples of use:
The [root@localhost ~] # declare-I Aban 20 Back3 #-I parameter can define the variable as an integer [root@localhost ~] # A=A+B # because it has been declared to be an integer, so it can be operated directly. [root@localhost ~] # echo $A # output View 237, $[] symbol operation
The format is basically as follows (as for operators, it is the same as other operation tools):
[root@localhost ~] # iTun4 [root@localhost ~] # iTunes $[root@localhost ~] # echo ${I} 7 [root@localhost ~] # echo $[root@localhost ~] # echo $[9echo 3] 3 [root@localhost ~] # echo $[10% 3] 1 [root@localhost ~] # echo $[1% 3] 1 [root@localhost ~] # echo $[3way 2] 1
-this is the end of this article. Thank you for reading-
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.