Numerical calculation of shell-variables 2

Source: Internet
Author: User

Use of the Let command

[Email protected] shell]# i=2

[[email protected] shell]# let i=i+8

[Email protected] shell]# echo $i

10

[Email protected] shell]# i=i+8

[Email protected] shell]# echo $i

10+8

[Email protected] shell]#

It can be seen from the above that if you do not use let, it is not calculated, we think of our calculation formula as a string to use

Usage of the expr command

The expr command is generally used for integer values, but can also be used in strings to evaluate the value of an expression variable, while expr is also a manual command-line evaluator, which means that it has its own computational function.

[[email protected] shell]# expr 2-2

0

[[email protected] shell]# Expr 2 + 2

4

[[email protected] shell]# Expr 2 \* 2

4

[[email protected] shell]# Expr 2 \ 2

1

[[email protected] shell]# Expr 2 \% 2

0

[Email protected] shell]#


That's how it's usually used in the shell.

[[email protected] shell]# result= "$ (expr 3 + 4)"

[Email protected] shell]# echo $result

7

Note: Operators have spaces around them, which is best used when using symbols to mask their specific meanings.

can be used for incremental calculations in loops, but generally we use the Let command, and if you use [] you can do without the empty brackets

[[email protected] shell]# expr $[2+3]

5

[[email protected] shell]# expr $[4+5]

9

Expr can be used to determine if a file is a suffix, such as

[[email protected] shell]# expr "id_dsa.pub": ". *.pub"

10

If it is a pub suffix, it will output the length of the file name string. If not, it will output 0.

You can tell if a variable is an integer,

[email protected] shell]# cat compute.sh

#!/bin/bash

Read-p "Please input:" A

Expr $a + 0 &>/dev/null

[$?-eq 0] && echo int | | echo chars

[Email protected] shell]# sh compute.sh

Please input:a

Chars

[Email protected] shell]# sh compute.sh

Please input:1

Int

[Email protected] shell]#

Usage of the BC command

BC is a calculator under UNIX, it can also be used under the command line, BC support scientific calculation, so often used, the general usage is as follows

[Email protected] shell]# echo 5.1+5|BC

10.1

[Email protected] shell]# echo 5.1+10|BC

15.1

[Email protected] shell]# seq-s "+" 100|BC

5050

[Email protected] shell]#

Scale refers to the retention of several decimal places

Obase is the conversion of decimal into binary

[Email protected] shell]# echo "scale=2;5.23/3.13" |BC

1.67

[Email protected] shell]# echo "scale=3;5.23/3.13" |BC

1.670

[Email protected] shell]# echo "Obase=2;8" |BC

1000

BC is characterized by the support of fractional operations.

You can look at the famous Yang Hui triangle (see the first kind of the main)

http://oldboy.blog.51cto.com/2561410/756234

This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1890019

Numerical calculation of shell-variables 2

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.