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

How to realize the Mathematical Operation of Linux Shell script

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to realize the mathematical operation of Linux Shell script". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Arithmetic operations are essential in any programming language, and shell is no exception.

First, use let, (()) and [] for arithmetic operations

You can use the normal variable assignment method to define a numeric value, which is, it is saved as a string. We can make these variables arithmetic by using operators such as let, (), [], and so on. For example:

The code is as follows:

#! / bin/bash

No1=4 # here no1 is stored as a semantic string

No2=5 # here no2 is stored as a semantic string

Let result=no1+no2

The output of echo $result # is 9

Let no1++ # is equivalent to let no1=no1+1

The output of echo $no1 # is 5

Let no2-- # is equivalent to let no2=no2-1

The output of echo $no2 # is 4

Let no1+=5 # is equivalent to let no1=no1+5

The output of echo $no1 # is 10

Let no1-=5 # is equivalent to let no1=no1-5

The output of echo $no1 # is 5

No1=4 # here no1 is stored as a semantic string

No2=5 # here no2 is stored as a semantic string

Result=$ [no1 + no2]

The output of echo $result # is 9

Result=$ [$no1 + 5] # ubuntu does not work properly, no1 not found

The output of echo $result # is 9

Result=$ ((no1 + 50)) # the $before parentheses cannot be lost, otherwise an error will be reported.

The output of echo $result # is 54

Third, use expr for arithmetic operations

The code is as follows:

Expr is a more advanced operator than the above three operators, which can alert you to advanced operations. For example:

#! / bin/bash

Result= `expr 3 + 4` # instead of using single quotation marks, it is the symbol of the key above the tab key

The output of echo $result # is 7

Every space between result= `expr 3 + 4` # 3 and + sign

The output of echo $result # is 3x4

Note: the methods in 1.5.1 and 1.5.2 can only calculate integers, not floating-point numbers.

Third, use bc for arithmetic operations

Bc is an advanced tool for mathematical operations. This precision calculator contains a large number of options, parameters are usually placed before the specific operation to be performed, and a semicolon is used as a delimiter to pass a bc through sdtin. For example:

Echo "4 * 0.56" | the output result of bc # is 2.24

The code is as follows:

No=54

Result= `echo "$no * 1.5" | bc`

The output of echo $result # is 81.0

Echo "scale=2;3/8" # passes the precision of scale tongue decimal, and the output result is 0.37

No=100

Echo "obase=2;$no" | bc # binary conversion. Target binary is binary, default primitive is 10.

No=1100100

Echo "obase=10;ibase=2;$no" | bc # obase=10,ibase=2

Echo "sqrt (100)" | bc # calculates square root

Echo "10 ^ 10" | bc # squared

-Note: obase is the target base, ibase is the primitive, and by default the primitive is 10. 0.

This is the end of the content of "how to realize the mathematical operation of Linux Shell script". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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