Arithmetic expression Operation Method Summary

Source: Internet
Author: User
Tags arithmetic numeric numeric value

Http://www.diybl.com/course/3_program/shell/shelljs/20120314/566065.html

1. Expr expression

Expr can use only one operator and does not support two or more operators
The expr command is generally used for integer values, but can also be used for strings. The General format is:
Expr argument operator argument
Expr is also a manual command line counter.
$EXPR 10 + 10
20
$EXPR 1500 + 900
2500
$expr 30/3
10
$expr 30/3/2
5
(Note that operators have spaces around them, and if no spaces indicate string connections)
When using multiplication sign, you must mask its specific meaning with a backslash. Because the shell may misunderstand the meaning of displaying asterisks.
$EXPR 30 * 3
90

Numerical test
You can test a number with expr. If you attempt to calculate a non-integer, an error is returned.
$RR =1.1
$expr $RR + 1
Expr:non-numeric argument
$RR =2
$expr $RR + 1
3
(Note: This example differs from the original text)
Here you need to assign a value to a variable (regardless of its content), perform a numeric operation, and import the output into Dev/null,
Then test the last command state, if it is 0, prove that it is a number, and others indicate a non-numeric value.
$value =12
$expr $value + >/dev/null 2>&1
$echo $?
0
This is a number.
$value =hello
$expr $value + >/dev/null 2>&1
$echo $?
2
This is a non-numeric character.

Expr can also return its own exit state, unfortunately the return value is exactly the opposite of the system's last Exit command,
The work returns 1, and any other value is invalid or error. The following example tests whether two strings are equal, where the string is
"Hello" and "Hello".
$value =hello
$expr $value = "Hello"
1
$echo $?
0
Expr returns 1. Don't confuse it, it shows success. Now check its final exit status and return 0 to indicate that the test was successful.
"Hello" is indeed equal to "hello".

Pattern matching
Expr also has a pattern-matching function. You can use expr to calculate the number of characters in a string by specifying the colon option.
He Gofu repeats 0 or more times.
$value =accounts.doc
$expr $value: '. * '
12

String matching operations can be used in expr, where patterns are used. d o c extracts file name.
$expr $value: ' (. *). Doc '
Accounts

1 x=1
2 x=$ (expr $x + 1)


There must be a space between $x + 1

Used to evaluate an arithmetic expression. The output of this command is sent to the standard output
Expr 4 + 5 will display on the screen 9

1. Note: There must be a space on either side of the operator 2.* must use \ ignore its specific meaning 3. Fractional parts are automatically ignored when decimals are not supported

2. Let expression
How let is used

x=10

Let x= $x +1

Let X+=1

Let x*=10, etc.

Let has no return value

3. Use $ (expression) or (expression) Form
How to use ((expression))

x=10

((x+=10))

((expression)) usage and let are similar

Usage of $ (()): $ ((x= $x +10))

Echo $x

y=$ ((x= $x-10))

Echo $y

y=$ (($x + 1))

Echo $y

Echo $x

4. Use $[] Form
Example: N=1

: $[n= $n +1] (between: and $ with a space)

y=$[n = $n + 1]

Echo $y

y=$[$n +1]

Echo $y

5. Using Decalare
Example:

Decare–i num

num= $num +1

Echo $num


Article Source: Flying Connaught Network (www.diybl.com): http://www.diybl.com/course/3_program/shell/shelljs/20120314/566065.html

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.