Shell arithmetic operation

Source: Internet
Author: User

(I = $ J + $ k ))  Equivalent to I = 'expr $ J + $ K'
(I = $ J-$ k ))   Equivalent I = 'expr $ J-$ K'
(I = $ J * $ k ))   Equivalent I = 'expr $ J \ * $ K'
(I = $ J/$ k ))   Equivalent I = 'expr $ J/$ K'

Let expressionsExecute one or more expressions. $ Is not required before the variable in the expression. If the expression contains spaces or other special characters, it must be caused.

For example, let "I = I + 1" or let I = I + 1

 

Arithmetic Operators are operators that can implement mathematical operations such as addition, subtraction, multiplication, and division in a program. The common mathematical operators in shell are as follows.

-+: Add two variables.

--: Subtract two variables.

-*: Multiply two variables.

-/: Divide two variables.

-**: Calculate the power of two variables.

-%: Modulo operation. The first variable is divided by the second variable to calculate the remainder.

-+ =: Add equals, and add the second variable on the basis of itself.

--=: Subtraction equals. The second variable is subtracted from the first variable.

-* =: Multiplication equals. Multiply the value of the first variable by the second variable.

-/=: Divide the Division by the second variable based on the first variable.

-% =: Modulo value assignment. The first variable performs modulo operation on the second variable, and then assigns the value to the first variable.

When using these operators, you must note the order of operation. For example, input the following command to output the result of 1 + 2.

Echo 1 + 2

Shell does not output result 3, but outputs 1 + 2. There are three methods in shell to change the operation sequence.

-Use expr to change the operation sequence. Echo 'expr 1 + 2' Can Be Used to output the result of 1 + 2. expr is used to represent the following expression as a mathematical operation. Note that 'is not a single quotation mark, but the symbol above the tab key.

-Use let to indicate mathematical operations. You can assign the result of the operation to variable B first. The operation command is B = Let 1 + 2. Then ECHO $ B is used to output the value of B. If there is no let, 1 + 2 is output.

-$ [] Indicates a mathematical operation. Write a mathematical operation to the brackets of the $ [] symbol. The content in the brackets performs mathematical operations first. For example, Echo $[1 + 2] will output result 3.

The following is an example of a shell program that implements the mathematical function S = 3 (xy) + 4x2 + 5y + 6. In the program, enter the values of x and y in the form of location variables. The procedure is 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 the insert mode, and then enter the following code.

Code 4-2Mathematical operation example: \ source file \ 04 \ 4.4.sh

#! /Bin/bash

# 4.4.sh

S = 0                                   # Define a sum variable. The initial value is 0.

T = 'expr $1 ** $2'                       # Use expr to change the operation order and obtain the power y of X.

T = $ [T * 3]                              # T multiplied by 3.

S = $ [S + T]                              # Add the result.

T = $[$1 ** 2]                            # Calculate the square of X.

T = $ [T * 4]                              # Multiply the result by 4.

S = $ [S + T]                              # Add the result.

T = 'expr $2*5'                         # Calculate the value of 5y.

S = $ [S + T]                              # Add the result.

S = $ [S + 6]                              # Add 6 to the result.

Echo $ s                               # Output result.

Echo $ (a % B ))                         # Remainder

 In this program, you need to pay attention to the writing of arithmetic operations. If the operation order is not changed without expr or $ [], the Operation formula is assigned a value in the form of a string without the result.

 Press ESC to return to normal mode. Enter ": W 4.4.sh" to save the file.

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

 On 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. Enter two parameters in the command.

./4.4.sh 2 4

 The program will complete the mathematical operations of S = 3 (xy) + 4x2 + 5y + 6 and output the results, as shown below.

90

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.