当前位置:网站首页>Shell basic syntax -- arithmetic operation

Shell basic syntax -- arithmetic operation

2022-06-12 08:35:00 __ Wanbo__

because shell Any data type is treated as string by default
So when performing arithmetic operations , Unlike other languages, it is not easy to add and subtract

Three arithmetic operation identifiers

(( ))	#  Recommended ,  Because use (()) Enclosed expression root C The language and grammar are the same ,  In line with daily use habits 
$[]		#  The result of the calculation is either assigned to a variable ,  Or use echo Print out 
expr	#  The calculation results are automatically printed to the terminal 

1. (( )) Arithmetic operation identifier

a=123
b=456

#  And C Arithmetic operations are used in the same way in languages 
#  The internal variables in double parentheses may not be preceded by $ operator 
# + You can add a space before and after the sign ,  You can also leave no spaces 
#  The return value is directly obtained by the equal sign 
sum=$(($a+$b))
sum=$((a + b))

echo $sum

#  Self increase and self decrease in double parentheses ,  There is no need to assign again 
((a++))
((a--))

#  The comma operator , ++  And  --  All operations and C Same language 
tt=$((a++, ++b))

#  Power operation 
sqrt=$((3**2))

#  You can do complex operations in double parentheses 
# (())  Variables in are declared automatically ,  There is no need to declare separately on the outside ,  After calculation, it can be used freely in the following 
for ((i=0; i<=100; i++))
{
    
    ((sum += i))
}

2. $[ ] Arithmetic operation identifier

# $[]  There must be a variable to accept the return value ,  Or use  echo  Print 
#  Otherwise it will be reported  command not found  error 
a=100
b=200

#  And C Arithmetic operations are used in the same way in languages 
#  stay [] You can add $ operator ,  Or you can skip it 
#  stay [] Can be found in + Add a space around the sign ,  Or you can skip it 
ret=$[a + b]

#  stay $[] Complex operations are not allowed in ,  Can't write like for $[i=0; i<100; i++]  In this way for Loop statement 
# $[] Multiple expressions in are separated by commas ,  The following expression will operate step by step from left to right according to the comma ,  The final value is assigned to ret
a=100
b=2
ret=$[a++, ++a, a**b]
#  The output is : 102 * 102 = 10404

3. expr Arithmetic operation identifier

# expr  It is the most regular and most arithmetic operation identifier ,  Automatically print after operation ,  It will not print automatically after using the command displacement 
# +  There must be spaces at both ends of operators such as sign ,  Without spaces, it will be recognized as a string 
#  When using variables ,  Variables must be preceded by  $  operator 
a=100
b=200

#  take expr When the operation result is assigned to a variable ,  To add a command substitution character 
sum=`expr $a + $b`

# expr  perform  *  Normal operation time ,  Think  *  The number is a wildcard ,  So you have to escape when you multiply 
#  Parentheses also need to be escaped ,  And the expression in parentheses should be filled with spaces 
a=100
b=200
ret=`expr $a \* \( $b + 2 \)`
echo $ret #  Output 20200

# expr  Cannot perform power operation 
#  Sum up , expr  The use of is simply useless ,  It's a lot of trouble ,  So it's not used in general expr Do arithmetic operations ,  Its advantage is string manipulation 
#  The correct posture is as follows :

# 1.  Find the length of the string 
expr length $str        #  There must be no spaces in the string 
# 2.  lookup   character   Position in string 
expr index $str "a"     #  There must be no spaces in the string 
# 3.  Intercepting string ,  Intercept from the starting position n Characters 
expr substr $str 3 4    #  There must be no spaces in the string 
# 4.  Returns the number of characters that are regular matches ,  Otherwise return to 0
expr match $str "^abc$"   #  There must be no spaces in the string 
原网站

版权声明
本文为[__ Wanbo__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120830015757.html