When you just learn to write a shell batch, you need to do the basic operations in the logic operation: four arithmetic, here is a simple implementation in the linux shell. method.
1.Simple Method
[chengmo@centos5 ~]$ b=$((5*5+5-3/2)) [chengmo@centos5 ~ ]$ echo $b29
In the linux shell, we can use $(())
to put the expression in parentheses to achieve the function of the operation.
2. Other methods:
Using :expr to achieve the operation
[chengmo@centos5 ~]$ expr 5 - 41
Note: Write the expression that needs the operation after expr, and ensure that there is a space between the parameter and the operation symbol.
Category Syntax Description Conditional judgment expr1 \\| Expr2 returns expr1 if expr1 is not zero or null, otherwise returns expr2. Expr1 \\& expr2 Returns expr1 if neither expr1 nor expr2 are zero or null, otherwise returns 0. The four-order operation expr1 + expr2 returns the value after expr1 plus expr2. Expr1 - expr2 returns the value after expr1 minus expr2. Expr1\\* expr2 returns the value after expr1 is multiplied by expr2. Expr1 /expr2 returns the value of expr1 except expr2. Expr1 % expr2 returns the remainder of expr1 except expr2. Size judgment expr1 \\> expr2 Returns 1 if expr1 is greater than expr2, otherwise returns 0. If both expr1 and expr2 are numbers, they are judged by the number size, otherwise they are judged by words. The following are the same. Expr1 \\< expr2 Returns 1 if expr1 is less than expr2, otherwise returns 0. Expr1 = expr2 Returns 1 if expr1 is equal to expr2, otherwise returns 0. Expr1 != expr2 Returns 1 if expr1 is not equal to expr2, otherwise returns 0. Expr1 \\>= expr2 Returns 1 if expr1 is greater than or equal to expr2, otherwise returns 0. Expr1 \\<= expr2 Returns 1 if expr1 is less than or equal to expr2, otherwise returns 0. Word processing expr1 : expr2 Compares a fixed string, which is a regular expression. The following characters can be used to assist:
. Matches one character.
$ Find the end of the string.
[list] Find any string that matches the list.
* Find 0 or more words before *.
\\( \\) Returns the string that matches the parentheses. .
3 floating-point operations:
[chengmo @ centos5 ~] $ expr 5.0 - 4 expr: Illegal Parameter
[chengmo@centos5 ~]$ echo $((5.0-4))-bash: 5.0-4: syntax error in expression (error token is ".0-4")< Br>
From the above results, it seems that the above expression is not enough to support floating-point operations. Looking at the data, I found out that bash does not support floating-point operations. If you need to perform floating-point operations, you need to use bc, awk processing.
Method 1:
[chengmo@centos5 ~]$ c=$(echo "5.01-4*2.0"|
Bc)[chengmo@centos5 ~]$ echo $c-2.99
Method 2:
[chengmo@centos5 ~]$ c=$(awk 'BEGIN{print 7.01*5-4.01 }')[chengmo@centos5 ~]$ echo $c31.04
Note: $() is equivalent to `` in the shell. The middle contains command statement execution and returns the execution result.
DevOps is the key to more and more large companies and organizations, so it is important for us to r
Running live55 under linux shows the figure: not getting ip: ip is 0.0.0.0 live555 does not know th
In order to ensure the normal operation of the Linux system and accurately solve various system prob
The network is often dropped. Many technicians think that the router is unstable. The ac
Nagios latest version installation under CentOS5.5
Linux disk use command du improved
Who is the most popular Linux web server
How to build Debian apt-get source on redhat
Tizen 1.0 released Linux-based mobile operating system
Enhanced Security Devil Linux 1.6.1 Release
Java in Chinese linux system folder appears Chinese garbled problem
Windows 7 and Fedora dual system installation method
Installing Arch Linux in my Defy
The role of CTRL+Z when running commands under Linux operating system
Two ways to install SUSE Linux10 system from hard disk
Ordinary users under Linux can not directly use crontab problem
Win7 system soudmax.dll error repair skills
Frequently Asked Questions for Win 8 System Automatic Updates
U disk installation Win7 system tutorial (a)
How to open control panel in win10
Tips for disabling the right mouse button when you cancel the web page
Win7 system notebook can not connect to WiFi, what should I do?
How to remove the arrow in the upper right corner of win10 icon
How to uninstall the application downloaded from the win10 app store