In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
There are several interesting commands to do math on the Linux system: the expr, factor, jot, and bc commands.
Can I do math under the Linux command line? Yes, of course! In fact, there are a number of commands that can easily do this, some of which even surprise you. Let's learn these useful mathematical commands or command syntax.
Expr
First of all, when it comes to using commands for mathematical operations on the command line, perhaps the easiest and most commonly used command is expr (the expression expression. It can perform four operations, and it can also be used to compare sizes. Here are a few examples:
Variable increment
$count=0$ count= `expr $count + 1` $echo $count1
Complete a simple operation
$expr 11 + 123134$ expr 13 / 1112$ expr 34-11123$ expr 11 * 123expr: syntax error 51$ expr 10\ > 990
Indeed, returns 1 and 0 indicate that the result of the comparison is true and false, respectively, which we generally expect on Linux. In the following example, using expr according to the above logic is incorrect, because if works on the opposite principle, that is, 0 represents true.
#! / bin/bashecho-n "Cost to us >" read costecho-n "Price we're asking >" read priceif [`expr $price\ > $cost`]; then echo "We make money" else echo "Don't sell it" fi
Next, let's run this script:
$. / checkPriceCost to us > 11.50Price we're asking > 6We make money
This is obviously not in line with our expectations! Let's modify it a little bit so that it works as we expect:
#! / bin/bashecho-n "Cost to us >" read costecho-n "Price we're asking >" read priceif [`expr $price\ > $cost` = = 1]; then echo "We make money" else echo "Don't sell it" fi
Factor
The function of the factor command is basically what you expected. You give a number, and the command gives the factor of the corresponding number.
$factor 111111: 3 37$ factor 134134: 2 67$ factor 1789417894: 2 23 389$ factor 19871987: 1987
Note: the factor command does not return more factors for the last number because 1987 is a prime number.
Jot
The jot command creates a series of numbers. Given the total number of numbers and the starting number.
$jot 8 101011121314151617
You can also use jot in the following way, where we want to decrement to the number 2.
$jot 8 10 2109875432
Jot can help you construct a list of numbers that can be used for other tasks.
$for i in `jot 7 17`; do echo April $I; doneApril 17April 18April 19April 20April 21April 22April 23
Bc
Bc is basically one of the best tools for command-line math. Enter the operation you want to perform and send it to the command using the pipeline:
$echo "123.4 percent 5Universe 6-(7.89 percent 1.234)" | bc113.664
It can be seen that bc does not ignore precision, and the input string is quite straightforward. It can also compare sizes, manipulate Boolean values, calculate square roots, sinusoids, cosines, tangents, and so on.
$echo "sqrt (256)" | bc16 $echo "s (90)" | bc-l.89399666360055789051
In fact, bc can even calculate pi. You need to specify the precision you need.
$echo "scale=5; 4yoga (1)" | bc-l3.14156$ echo "scale=10; 4yoga (1)" | bc-l3.1415926532$ echo "scale=20; 4yoga (1)" | bc-l3.14159265358979323844$ echo "scale=40; 4yoga (1)" | bc-l3.1415926535793238463433835028841968
In addition to receiving data and returning results through a pipeline, bc can run interactively by entering the operations you want to perform. The scale setting mentioned in this example specifies the number of significant digits.
$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'.scale=23/4.752/3.66quit
You can also use bc to complete the digital base conversion. Obase is used to set the numeric base of the output.
$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'.obase=1616 fix > y $((xxxx11)); ((yong7)); ((zodi3)) $if ((x > y)) > > ((y > z)); then > echo "letters roll downhill" > filetters roll downhill
Or as follows:
$if [x > y] z]; then echo "letters roll downhill"; filetters roll downhill
The following calculates the third power of 2:
$echo "2 ^ 3" 2 ^ 3$ echo "2 ^ 3" | bc8
Summary
In Linux systems, there are many different command-line tools that can perform numeric operations. I hope you can master one or two new tools after reading this article.
All right, that's all of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. If you have any questions, you can leave a message and exchange. Thank you for your support.
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.