In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
If you want to perform arithmetic operations, you can't do without a variety of operation symbols, and like other programming languages, Shell also has many arithmetic operators.
Common operation symbols, such as figure:
Common operation commands, such as figure:
The following is a detailed description of the detailed usage of various operation commands:
1. Double parentheses "()" numerical operation command
The function of double parentheses "()" is to compare numerical operations with numerical values. It has high efficiency and flexible usage. It is an operation operator often used by operators in enterprise scenarios. Specific operation syntax, as shown in the figure:
Describe its usage in detail:
Perform a simple operation as follows:
[root@localhost ~] # echo $((1x1)) 2max / calculate the result of 1x1, and then enter [root@localhost ~] # ((ipolar 5)) [root@localhost ~] # ((i=i*2)) [root@localhost ~] # echo $i10bind / first define the value of I, then perform the operation, and finally output the result.
A slightly more complex synthesis operation is as follows:
[root@localhost ~] # ((aq1 / 2) / 3-4% 3) & & echo $a8bind / "* *" means power, "%" means that the remainder is multiplied, divided and then added and subtracted, and / / the result is output through echo
Examples of special symbolic operations:
[root@localhost ~] # axi1 [root@localhost ~] # echo $((a=a+1)) 2max / reassign "axi1" to a, and then output the result [root@localhost ~] # echo $((astat.1)) 3pm / "+ =" means "a=a+1", and then copy it back to a [root @ localhost ~] # echo $((a=a+1 2)) 9pm / "* *" represents the power, now the value of an is 3, of course the square is 9.
Use "()" double parentheses to compare and judge examples:
[root@localhost ~] # echo $((1/dev/null [root@bogon ~] # echo $? 2
Example script:
The first kind:
[root@bogon ~] # vim test.shroud then echo int else echo charsfi then echo int else echo charsfi [root@bogon ~] # sh-x test.sh 12 + 1 +'['0-eq 0']'+ echo intint [root@bogon ~] # sh-x test.sh qq+ expr qq+ 1 +'[2-eq 0']'+ echo charschars
The second kind
[root@bogon ~] # vim test1. Root@bogon sh test1.shPlease input:1intPlease input:qchars// sh test1.shPlease input:1intPlease input:qchars// is always waiting for input and determines that scripts such as An expr $A + 1 & > / dev/null if [$?-eq 0] then echo int else echo chars fidone [root@bogon ~] # sh test1.shPlease input:1intPlease input:qchars// are always waiting for input / / if you only want to judge once and then end, delete the while loop statement! 4. Calculate the length of a string through expr [root@bogon ~] # a=dfdfdfdfdfd [root@bogon ~] # expr length "$a" 11 [root@bogon ~] # echo ${# a} 11 [root@bogon ~] # echo ${a} | wc-L11 [root@bogon ~] # echo ${a} | awk'{print length ($0)}'11 IV. Usage of bc command
Bc is a computer under Linux. It can be used not only as a computer, but also as a command tool!
The bc command is used as a computer:
[root@bogon] # bcbc 1.06.95Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'. 1: 23
The bc command calculates on the command line:
[root@bogon ~] # echo 3 places 5 | bc8 [root@bogon ~] # echo "scale=2;355/113" | bc3.14// uses scale=2 to retain two decimal places. Fifth, awk implementation of computing
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. For example:
[root@bogon ~] # echo "1.21.3" | awk'{print $113,2} '2.5 [root@bogon ~] # echo "10 10" | usage of awk' {print ($1x 10) / $2} '2VI, $[]
Example:
[root@bogon ~] # echo 10 [root@bogon ~] # echo $[2-3]-1 & & echo $i20 [i20] # echo $[2-3] 6 [root@bogon ~]-1
So much for a brief introduction this time!
-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
Linux Open Source Please add a link description
© 2024 shulou.com SLNews company. All rights reserved.