Usage of Shell expr
Mathematical operations
Let command expr command BC command $ (()) $[]
Http://www.80ops.cn/archives/245.html
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 is equivalent to echo + 10|BC
20
Expr 1500–900 is equivalent to echo 1500–900 |BC
2400
Expr 30/3 is equivalent to echo 30/3|BC
10
Expr 30/3/2 is equivalent to echo 30/3/2 |BC
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 is equivalent to echo \* 3 |BC
90
For arithmetic assignment operations
val2=10
val1=$ (($val 2–1)) is equivalent to val1=$ (expr $val 2–1) equivalent to val1=$ (Echo $val 2–1|BC)
echo $val 1
It's more of a notation
The most important thing about expr is the powerful string manipulation
1. Extract the subscript of the specified character: Expr index content character
For example
ind= ' expr index ' 69lki ' "K" '
Echo $ind
4
ind= ' expr index ' 69lki ' "K" '
Echo $ind
4
2. Extract string substring: expr substr content start position End position
For example
con= ' expr substr ' 689sdfwe "" 1 "3 '
Echo $con
689
con= ' expr substr ' 689sdfwe "" 1 "3 '
Echo $con
689
Ash Shell let command (2008-10-21 10:18:43) reprint
Http://blog.sina.com.cn/s/blog_5ac88b350100arnv.html
Tags: gossip
Integer operations are generally implemented through the two instructions of let and expr, such as the variable x plus 1 can be written: let "x = $x + 1" or x= ' expr $x + 1 ', personal feeling, the two similar, the former expression is relatively simple, the use of merit
In bash, let can be used to do numerical operations
bash-2.05b$ Export n=20
bash-2.05b$ n=n+1
bash-2.05b$ Echo $n
N+1
N+1 does not follow digital processing, but installs string concatenation processing
If you want to implement N as a value, you need to let go.
bash-2.05b$ Export n=20
bash-2.05b$ Let N+=1
bash-2.05b$ Echo $n
In a bash shell, you can only perform an operation between two integers, and the result is still an integer. To perform arithmetic
operation, you need to use the Let command, which is syntax:
Let expr
Expr is an expression that contains an item and an operator, which can be a variable or an integer constant.
When an integer constant is used, it defaults to a decimal integer, and the user can use Radio#number to specify additional
An integer of the form, where radio defines the integer as a binary representation, and number is the value of the integer. If
RADIO>10, then the numeric characters can be from 0-9 and A-Z.
The operators that are supported in an expression and their meanings are:
+,-, *,/,% Plus, minus, multiply, divide, modulo
>>,<<,&,^,| Move left, shift right, bit to, bit XOR, bit or
?: Ternary operator. Consistent with the definition in the C language
~ Take the complement
!,>=,<=,>,<,==,!=,&&,| |
=,+=,-=,*=,/=,%=,<<=,>>=,&=,^=,|=
Parentheses can also be used in expressions. The definition of parentheses or the precedence of operations is defined in the general computer language.
Same.
The Let command has a return value. When the result of the calculation (if there are multiple expressions, whichever is the last) is 0 o'clock,
The return value is 1, otherwise 0.
When an expression contains a special character of the shell (such as |) , you need to reference it with a reference character (' or ').
When using let, the shell calculates the x+y value for a x+y such as let.
But discard the result, and if you don't want to, you can use let sum=x+y to save the x+y result in the variable sum
You can also use the ((and)) operator instead of the Let command, and in this case you can omit the arithmetic
A reference to an expression that requires a $ (()) format if you want to return the value of an expression.
If in the condition, "=" is used to compare strings; "-eq" is used to compare integer numbers.
Usage of Shell expr