The calculations inside the shell

Source: Internet
Author: User
Tags arithmetic bitwise

---restore content starts---

The calculation of the shell is actually already in contact with Linux. Every time in the use of the Internet is looking everywhere, so think to spend some time to study.

First understand the following commonly used arithmetic operation symbols:

+  -

Addition (plus), subtraction ( minus sign )

*  /  %

Multiplication, division, take-over

**

Power operation

++ --

Increase or decrease, can be placed before the end of the variable

! && | |

logical non (inverse). Logical AND (and), logical OR (or)

< <= > >=

Less than or equal to greater than or equal to

== !=  =

Compare symbols (equal.) Not equal. for string = also equals sign)

<< >>

Shift left, shift right

~ | & ^

The position is reversed bitwise XOR or bitwise AND bitwise OR

= += -= *= /= %=

Assignment operators such as a+=1 are a=a+1

The usual arithmetic operations commands are as follows

Arithmetic operations and Operations commands

Significance

(())

Commonly used operators for certificate operations, which are highly efficient

Let

For certificate operations , similar to ' (()) '

Expr

can be used for integer operations, but there are many additional features

Bc

an operational program (number and certificate and fractional arithmetic) under Linux

$[]

For integer operations

Awk

awk can be used either for certificate operations or for decimal operations

Declare

Define variables and properties , the- i parameter can be used to define shaping variables , do operations /

One: The operation practice of the double parenthesis

1.1

using (()) A simple calculation ((i=i+1)) means assigning the result of the i+1 to I, but if I want to refer to the result, I do not know that Echo ((i=1+1)) must be added with the $ sign echo $ ((i=1+1))

    

     

1.2 Use (()) double brackets to compare and judge

The 1 representation here is true, and 0 means that false can be combined with logical operations.

Note: The numbers involved above and the variables must be integers (shaped), not decimals or strings

1.3 Usage of--,++

To give the simplest example:

++a indicates the number of outputs computed, a++ indicates that the output is a value

Assignment operation of 1.4 (())

Note here is (()) that the reference variable can not add $ (of course, you can add, I used to add)

(()) There are no spaces between all the characters, one or more of them will not affect the result. The Let Command and (()) function the same but less efficient.

Usage of the two expr commands

Expr can be used either for integer calculations or for the length of a string, and for matching arithmetic processing.

2.1 For calculation

    

2.2 is used for strings, and expr determines whether a string is an integer. Because the function is cubby less, it is difficult to judge whether a string is an integer. The implementation principle, the addition of a string and 1 to see the return value, if the return value is 0, then this number is an integer

Note that there must be a space before and after you use expr to calculate the symbol!!

You can also use match to match the following:

The special use of expr to determine if the file name meets the requirements!

Calculate the length of a string

AWK implements the calculation!

A very good way to implement calculations with awk, especially decimals

  

All of us are quoted directly using $i, or can be defined using declare (same as typeset) and then used directly

It's less used.

$[] The operation of the symbol

  

The above is our common operation!! Here are a few examples

Write a script, complete the input two number, determine whether the two number is an integer, if it is an integer, complete the addition and output, if not then give the corresponding prompt

     

   #!/bin/bashRead-T 15-p"Please input the num:"a b [${#A}-le 0] && {echo "the first num is null exit 1"}[${#B}-le 0] && {echo "the Secound num is null exit 1"}expr $a+ 1 &>/dev/NULL retval_a=$? Expr $b+ 1 &>/dev/NULL Retval_b=$? if[$RETVAL _a-ne 0-o $RETVAL _b-NE 0]; then echo"One of the NUM is not num, pls input again"Exit1fi

---restore content ends---

The calculations inside the shell

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.