Let command introduction:
Let command allows the bash shell to perform arithmetic operations, using lets to compare two operational values or perform operations such as subtraction, which are often used in the process control structure of the shell program or perform the required operations, noting that let can only perform the related operations of integers, The result of an operation can only hold integers.
Use the following methods:
Let variable name = variable 1 operator variable 2
Common Arithmetic Operations Categories:
Addition: +
Subtraction:-
Division:/
Multiplication: *
Take remainder:%
Let command use Method Example:
Note: Similar to the C language, let i= $i +1 can be written as lets i++ to simplify writing
Shell Instance 1:
[Root@changerlee operator comparison] #cat let_op.sh
#!/bin/bash
#some Examples about let
num1=106
num2=10
Num3=5
#oprating let-
res1=${num1}+${num2}+${num3} let-
res2=${num1}-${num2}-${num3} let
res3=${ NUM1}*${NUM2}*${NUM3} let-
res4=${num1}/${num2}/${num3} let
res5=${num1}%${num2}%${num3}
#output Results
echo "num1+num2+num3=${res1}"
echo "Num1-num2-num3=${res2}"
echo "Num1*num2*num3=${res3}"
echo "Num1/num2/num3=${res4}"
echo "num1%num2%num3=${res5}"
[Root@changerlee operator comparison] #sh let_op.sh
num1+num2+num3=121
num1-num2-num3=91
num1*num2*num3=5300
num1/num2/num3=2
num1%num2% Num3=1
Note that num1/num2/num3 gets the result to be an integer 2, which means that let can only hold an integer result and intercept the fractional part
Shell Instance 2:
[Root@changerlee operator comparison] #cat let_op_v1.sh #!/bin/bash # Let and while
i=10
num=1 while
[1]
do
if [$num-le $i]
then
echo "$num"
else
break
fi let
num= $num +1
done
[ Root@changerlee operation comparison] #sh let_op_v1.sh
1
2
3
4
5
6 7 8 9 10
Let does not apply to the operation between decimals:
Shell Instance 3:
<span style= "FONT-SIZE:18PX;" ><strong>[root@changerlee operation Comparer] #let 1+1
[Root@changerlee operator comparison] #let 1.1+1
-bash:let:1.1+1: Syntax error:invalid arithmetic operator (Error token is ". 1+1") </strong></span>
Introduction to the expr command:
Expr is similar to let function in Linux commands when it is arithmetic, can only do integer type operation, cannot save decimal result. Expr can also perform operations between strings.
Expr is used in the following ways:
Expr expression1 Operator Expression2
The operator must be used to escape, and the operator and the two expression must have a space between them (this is different from let) and expr is not suitable for decimal operations.
Common Arithmetic Operations Categories:
Addition: +
Subtraction:-
Multiplication: *
Division:/
Take remainder:%
Examples of how expr is used:
(1) Perform the usual arithmetic operations:
Shell Instance 4:
[Root@changerlee operator comparison] #cat expr_op_v1.sh
#!/bin/bash
#an exmple of expr
num1=56
num2=10
echo "NUM1: $num 1 num2: $num 2 num3: $num 3"
#operating num
res1= ' expr $num 1 \+ $num 2 \+ $num 3 '
res2= ' expr $num 1 \-$num 2 \-$num 3 '
res3= ' expr $num 1 \* $num 2 \* $num 3 '
res4= ' expr $num 1 \/$num 2 \/$num 3 '
res5= ' expr $num 1 \% $num 2 \% $num 3 '
#output results
echo ' num1+num2+num3= $res 1 "
echo" num1-num2-num3=$ Res2 "
echo" num1*num2*num3= $res 3 "
echo" num1/num2/num3= $res 4 "
echo" num1%num2%num3= $res 5 "
[ Root@changerlee operation comparison] #sh expr_op_v1.sh
num1:56 num2:10 num3:5
num1+num2+num3=71
num1-num2-num3=41
num1*num2*num3=2800
num1/num2/num3=1
num1%num2%num3=1
</pre><p><strong><span style= "Font-family:microsoft yahei;font-size:18px;" >shell instance 5:</span></strong></p><p><strong><span style= "Font-family:Microsoft yahei;font-size:18px; " ></span></strong></p><pre name= "code" class= "HTML" >[root@changerlee operator comparison] #cat Expr_op_ v2.sh
#!/bin/bash
# Let and then
i=10
num=1 while
[1]
do
if [$num-le $i]
then
E Cho "$num"
else
breaks fi
num= ' expr $num \+ 1 '
done
[Root@changerlee operator comparison] #sh Expr_op_ v2.sh
1
2
3
4
5
6 7 8 9 10
Shell Instance 6:
[Root@changerlee operator comparison] #expr 1 \+ 1
2
[Root@changerlee operator comparison] #expr 1.1 \+ 1
expr:non-integer argument
(2) perform common string operations
1. Length of output string
<strong><span style= "FONT-SIZE:18PX;" >[root@changerlee operation comparison] #cat expr_str.sh
#!/bin/bash
#output length str
str1= "This str length is 21"
str2= "Blog.csdn.net/changerjjlee"
Echo ' ${#str1} ' ${#str1}
expr length $str 2
[Root@changerlee Operation Comparison] #sh expr_str.sh
${#str1}21
26</span></strong>
Note: Expr can only output strings that do not contain spaces
2. The operation of the string
Expr substr $string $postion $length location number starting from 1
echo ${$string: $postion: $length} location number starting from 0
Shell Instance 7:
[Root@changerlee operator comparison] #string = "Abcdefghi"
[Root@changerlee operator comparison] #expr substr $string 1 3
ABC
[ Root@changerlee operation comparison] #echo ${string:0:3}
ABC
(3) operation of string concatenation
Shell Instance 8:
<strong>[root@changerlee operation comparison] #cat con_str_v1.sh
#!/bin/bash
#connection of Strings
str1= "abc"
str2= "def ghi"
str3= "${str1} $str 2"
echo "str1= $str 1"
echo "str2= $str 2"
echo "str3= $str 3"
[Root@changerlee operation Comparer] #sh con_str_v1.sh
str1=abc
str2=def ghi
str3=abcdef ghi</strong>
(4) operation of string substitution
Shell Instance 9:
<strong><span style= "FONT-SIZE:18PX;" >[root@changerlee operation comparison] #cat con_str_v2.sh
#!/bin/bash
string= "Blog.csdn.net/changerjjlee"
Echo ${ STRING/C/C} #只替换一次
echo ${string//c/c} #全部替换
[Root@changerlee operator comparison] #sh con_str_v2.sh
Blog. Csdn.net/changerjjlee
Blog. Csdn.net/changerjjlee
</span></strong>