Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the mathematical calculations in shell?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you what mathematical computing is about in shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Examples of wrong methods

a)

Var=1+1

Echo $var

The result of the output is 1: 1, tragedy, hehe

b)

Var=1

Var=$var+1

Echo $var

The output result is 1: 1, which is still tragic, hehe.

2. The correct method

1) use let

Var=1

Let "var+=1"

Echo $var

The output is 2, and there is no tragedy this time.

Note:

A) after my test, let supports almost all operators. I read an article on the Internet that "let does not support +, -, comma, (,)", but after my test, self-addition, self-subtraction, and parenthesis priority are well supported.

B) the exponentiation should use "*"

C) parameters are accessed directly in the expression without adding $

D) in general, arithmetic expressions can be without double quotation marks, but if there are keywords in bash in the expression, you need to add them.

E) expressions after let can only operate on integers

2) use (())

Var=1

((var+=1))

Echo $var

The output is 2.

Note:

The method of using () is exactly the same as that of let.

3) use $[]

Var=1

Var=$ [$var+1]

Echo $var

Output result bit 2

Note:

A) $[] calculate the result before outputting the expression in parentheses as a mathematical operation

B) you need to add $before accessing the variables in $[]

C) $[] supports the same operators as let, but only supports integer operations

4) use expr

Var=1

Var= `expr $var + 1`

Echo $var

The output is 2.

Note:

A) the symbols of expressions after expr need to be separated by spaces

B) the operators supported by expr are:, &, +, -, *, /,%

C) the operators supported by expr need to be escaped using\: |, &, *

E) expr also only supports integer operations

5) use bc (floating point calculation can be performed)

Var=1

Var= `echo "$var+1" | bc`

Echo $var

The output is 2.

Introduction:

Bc is a simple calculator under linux, which supports floating-point calculation. Enter bc under the command line and enter the calculator program. When we want to calculate floating-point numbers directly in the program, we can use a simple pipeline to solve the problem.

Note:

1) I have tested that bc supports all operators except bitwise operators.

2) scale should be used for precision setting in bc

3) floating point calculation example

Var=3.14

Var= `echo "scale=2;$var*3" | bc`

Echo $var

The output is 9.42

6) use awk (floating point calculation can be done)

Var=1

Var= `echo "$var 1" | awk'{printf ("% g", $1 percent echo 2)}'`

Echo $var

The output is 2.

Introduction:

Awk is not only a text processing tool, but also a programming language, as a programming language, awk supports a variety of operations, and we can use awk for floating-point calculation, just like the above bc, through a simple pipeline, we can directly call awk in the program for floating-point calculation.

Note:

1) awk supports all operators except micromanipulation operators

2) awk has built-in log, sqr, cos, sin and other functions.

3) floating point calculation example

Var=3.14

Var= `echo "$var 2" | awk'{printf ("% g", sin ($1ax 2))}'`

Echo $var

The output is 1

Thank you for reading! This is the end of this article on "what is Mathematical Computing in shell?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report