Mainly includes the following 3
String manipulation
Mathematical operations
Floating point Arithmetic
One. String manipulation
Connection of Strings
Connection Word 2 strings do not need any connectors, write next to each other
Length acquisition
Expr Length "Hello"
Expr Length "$str" variable name must be placed in double quotation marks, no person syntax error
find the position of a character in a string
Expr index "$STR" CHARS
The first one is starting from 1, finding no return 0, returning the position of the first character to match to
[Email protected] ~]#Echo$strhello Word[[email protected]~]#ExprIndex"$str"h1[[Email protected]~]#ExprIndex"$str"Hel (Match h only)1[[Email protected]~]#ExprIndex"$str"a0
String truncation
Expr substr "$str" POS LENGTH
POS start position (included), length
Expr " $str " 7 4 Word
String match
Expr "$str": REGEXP (spaces before and after a colon)
Expr mathch "$str" REGEXP
Must be fully matched.
echoexpr"$str"\ ([a-z]* [a-z]*\)] ABcD PhP
Two. Mathematical operations
Logical operations
Numeric operations
Logical operations
&,|,<,>,=,!=,<=,>=
Numeric operations
+,-,*,/,%
Expr expression
Result=$[expression]
SH Echo $num 1, $num 2, $num 3 1,2,1shexpr $num 1<$num 22: No file or directory
There are spaces on both sides of the operator
[[Email protected]SH]#Expr$num 1\<$num 21<2[[Email protected]SH]#Expr$num 1 \<$num 21[[Email protected]SH]#Expr$num 1 =$num 31[[Email protected]SH]#Expr$num 1 =$num 20used in expr=to determine whether to wait in the []==
[Email protected] sh]# res=$[$num 1= $num 3]
-bash:1=1:attempted Assignment to Non-variable (Error token is "= 1")
[Email protected] sh]# res=$[$num 1== $num 3]
[Email protected] sh]# echo $res
1
Floating-point arithmetic
Built-in calculator BC
BC is able to identify:
Numbers (integer and floating point types)
Variable
Comments (in #开始的行 or/* */)
An expression
Programming statements (e.g. conditional judgment: If-then)
Function
Bc-q can ignore the version information and other prompt language
Scale can set precision
SH] # BC-qten/33scale =4/33.3333 num1=ten; num2=3num1/num23.3333quit
Using BC in Scripts
1.
Var= ' echo ' options;expression ' |BC '
[[email protected] sh ]# res= ' echo " SCALE=4;10/3 " | BC "[[email protected] sh ]# echo $res 3.3333 2 .res = ' Bc<<eoptionsstatementsexpressionse '
SH] # res= ' BC <<E> a=> b=3> scale=4> c=a/b> C >shecho $res3.3333
Operation of variables in shell programming (Shell 06)