Linux Shell Programming (5): integer arithmetic

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Http://blog.sina.com.cn/s/blog_6db275da0101asmf.html

((i= $j + $k)) equivalent to i= ' expr $j + $k '
((i= $j-$k)) equivalent to i= ' expr $j-$k '
((i= $j * $k)) equivalent to i= ' expr $j \* $k '
((i= $j/$k)) equivalent to i= ' expr $j/$k '


Let expressions executes one or more expressions. You do not have to have $ before a variable in an expression. If the expression contains spaces or other special characters, it must be caused.

Example: let "i = i + 1" or let i=i+1

Arithmetic operators refer to operators that can implement addition, subtraction, multiplication, and other mathematical operations in a program. The mathematical operators commonly used in the shell are as follows.

-+: Add to two variables.

--: Subtract two variables.

-*: Multiply two variables.

-/: Divide two variables.

-**: A power operation on two variables.

-%: Modulo operation, the first variable divided by the second variable to find the remainder.

-+=: Plus equals, adds a second variable on its own basis.

--=: Minus equals, minus the second variable on the basis of the first variable.

-*=: Multiply equals, multiplied by the second variable on the basis of the first variable.

-/=: In addition to equals, divides the second variable on the basis of the first variable.

-%=: Modulo assignment, the first variable takes a modulo operation on the second variable, and assigns the value to the first variable.

When using these operators, you need to be aware of the problem of the order of operations. For example, enter the following command to output the result of the 1+2.

Echo 1+2

The shell does not output the result 3, but instead outputs the 1+2. There are three ways to change the order of operations in the shell.

-Change the order of operations with expr. You can use echo ' expr 1 +2 ' to output the result of the 1+2, and use expr to represent the following expression as a mathematical operation. Note that ' It's not a single quote, it's the symbol above the TAB key.

-use let to indicate mathematical operations. The result of the operation can be assigned to the variable B, and the Operation command is B=let 1 + 2. Then use echo$b to output the value of B. If there is no let, the 1+2 is output.

-use $[] to represent mathematical operations. Writes a mathematical operation to the brackets in the $[] symbol, and the contents of the brackets are mathematically calculated first. For example, command echo$[1+2], the result 3 will be output.

The following is a shell program instance that implements the math function s=3 (XY) +4x2+5y+6 operation. Enter the values of x and y as positional variables in your program. The steps to write the program are as follows.

Open a terminal in the main menu. Enter the VIM command in the terminal to open vim.

Press the "I" key in Vim to enter insert mode and enter the following code.

Code 4-2 Example of a mathematical operation: \ source file \04\4.4.sh

#!/bin/bash

#4.4.sh

S=0 #定义一个求和变量, the initial value is 0.

t= ' expr$1**$2 ' #用expr改变运算顺序, seeking X's Y-square.

T=$[T*3] #t乘以3.

S=$[s+t] #结果相加.

T=$[$1**2] #求x的平方.

T=$[T*4] #结果乘以4.

S=$[s+t] #结果相加.

t= ' expr$2*5 ' #求5y的值.

S=$[s+t] #结果相加.

S=$[S+6] #结果加上6.

Echo$s #输出结果.

echo$ ((a%b)) #取余

In this program, you need to pay attention to the notation of arithmetic operations. If no expr or $[] changes the order of operations, the expression is assigned as a string, without assigning the result of the operation.

Press "ESC" key to return to normal mode. Then enter ": W 4.4.sh" to save the file.

Enter ": Q" command and press "enter" key to exit vim.

In the terminal, enter the following command to add executable permissions to the 4.4.sh file.

chmod +x 4.4.sh

Enter the following command to run the program. You need to enter two parameters in a command.

./4.4.sh 2 4

The program completes the mathematical operation of the s=3 (XY) +4x2+5y+6 and outputs the results, as shown below.

90

Linux Shell Programming (5): integer arithmetic

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.