In the shell environment, the following methods are supported for mathematical or logical operations.
- Using the expr command to perform mathematical or logical operations directly, such as
<span><span>expr 2 + 2</span></span>. - Using the bc command, data is piped into the bc command for calculations, for example
<span><span>echo "scale=4; 3.44/5" | bc</span></span>. - Using the
<span><span>$ and $[operation]</span></span>format brackets for mathematical operations, for example<span><span>$[2+2]</span></span>, or directly<span><span>[operation]</span></span>combined with if, while for logical operations. - Using the
<span><span>$ and $((operation))</span></span>format double brackets for mathematical operations, for example<span><span>$((2+2))</span></span>.
Specific examples of the above methods are shown below.
# expr command
var1=$(expr 2 + 2)
echo "var1: $var1" # Output: var1: 4
# bc command
var2=$(echo "scale=4; 3.44/5" | bc)
echo "var2: $var2" # Output: var2: .6880
# $ and $[operation] format brackets for mathematical operations
var3=$[2+2]
echo "var3: $var3" # Output: var3: 4
# $ and $((operation)) format double brackets for mathematical operations
var4=$((2+2))
echo "var4: $var4" # Output: var4: 4
Operator Types
The previously discussed common commands and formats for mathematical operations support several types of operators, including arithmetic operators, comparison operators, and logical operators; these can be used for numerical calculations, logical branching, and loop statements, forming one of the foundations for implementing shell scripts. The specific details are as follows.
- Arithmetic Operators
| Symbol | Description | Example |
|---|---|---|
| +、-、*、/ | Addition, Subtraction, Multiplication, Division | <span><span>$((2+2))</span></span> |
| ++、– | Increment, Decrement | <span><span>$((i++))</span></span> |
| ** | Exponentiation | <span><span>$((2**3))</span></span> |
| % | Modulus | <span><span>$((2%3))</span></span> |
| = | Assignment | <span><span>$((a=2))</span></span> |
- Comparison Operators
| Symbol | Description | Example |
|---|---|---|
<span><span>-eq、==</span></span> |
Check if numbers are equal | <span><span>[ $a -eq $b ]</span></span> |
<span><span>-ne、!=</span></span> |
Check if numbers are not equal | <span><span>[ $a -ne $b ]</span></span> |
<span><span>-gt、></span></span> |
Check if left is greater than right | <span><span>[ $a -gt $b ]</span></span> |
<span><span>-lt、<</span></span> |
Check if left is less than right | <span><span>[ $a -lt $b ]</span></span> |
<span><span>-ge、>=</span></span> |
Check if left is greater than or equal to right | <span><span>[ $a -ge $b ]</span></span> |
<span><span>-le、<=</span></span> |
Check if left is less than or equal to right | <span><span>[ $a -le $b ]</span></span> |
- Logical Operators
| Symbol | Description | Example |
|---|---|---|
<span><span>!</span></span> |
Logical NOT | <span><span>[ !false ]</span></span> |
<span><span>-o、||</span></span> |
Logical OR | [ <span><span>$a -eq $b -o !true ]</span></span> |
<span><span>-a、&&</span></span> |
Logical AND | <span><span>[ $a -eq $b -a !true ]</span></span> |
<span><span>-z</span></span> |
Check if string is empty, returns true if 0 | <span><span>[ -z " " ]</span></span> |
<span><span>-n</span></span> |
Check if string is not empty, returns true if not 0 | <span><span>[ -n " " ]</span></span> |
<span><span>$</span></span> |
Check if string is not empty, returns true if not empty | <span><span>[ $var ]</span></span> |
Specific examples of operator usage are shown below.
# + - * / **
var5=$((2+5-3*2))
echo "var5: $var5"
var6=$((2**3 - 6/3))
echo "var6: $var6"
## ++ %
var7=$((var5++))
echo "var7: $var7 var5: $var5"
var8=$((var6%var5))
echo "var8: $var8"
# == > < <= >= !=
var9=$[ $[ $var5 == $var6 ] || $[ $var5 > $var6 ] ]
var10=$[ $var5 <= $var6 ]
echo "var9: $var9 var10: $var10"
# -le -eq
if [ $var5 -le $var6 ] || [ $var5 -eq $var6 ]; then
echo "$var5 <= $var6"
fi
# file test
[ -f "test/err.log" ] && echo -n Y || echo -n N
echo -n " "
[ "test/err.log" -ef "test/err2.log" ] && echo -n Y || echo -n N
echo -n " "
[ "test/err.log" -nt "test/err2.log" ] && echo -n Y || echo -n N
echo -n " "
[ "test/err.log" -ot "test/err2.log" ] && echo -n Y || echo -n N
echo -n " "
[ "test/err.log" -ot "test/errNil.log" ] && echo -n Y || echo -n N
echo ""
# Using expr command for mathematical operations
let var11=`expr $var5 + $var6`
echo "var11: $var11"
# Using bc command for complex mathematical operations
# scale=2 means the result is kept to two decimal places
var12=$(echo "scale=2; 10/3" | bc)
echo "var12: $var12"
For the above statements, the logical operators<span><span>AND(&&)</span></span> and <span><span>OR(||)</span></span> require further explanation, as detailed below.
- AND
<span><span>&&</span></span>: The AND statement executes the next only if the previous one is satisfied; if any one is not satisfied, it exits execution and returns the status; if all are satisfied, it returns true. - OR
<span><span>||</span></span>: The OR statement executes the next only if the previous one is not satisfied; if any one is satisfied, it exits execution and returns the status; if all are not satisfied, it returns false.
Regarding the operations of logical operators, specific examples are shown below.
# AND statement execution, the next executes only if the previous one is satisfied
statement1 && statement2 && ... && statementX
# OR statement execution, the next executes only if the previous one is not satisfied
statement1 || statement2 || ... || statementX
# Example
# Change to uboot directory, if unsuccessful return
cd "${PLATFORM_UBOOT_PATH}"/ || return
# For && and ||, you can use {} to construct statement blocks.
# Test constructing statement blocks
ls -alF && {
echo "block test"
ps -ef | grep ls
}
Summary
Mathematical operations and logical statements form the technical foundation for numerical calculations and branching in shell language; they can create complex judgments and processing, which are essential for perfecting shell scripts and are crucial features. Systematic learning and mastery of these concepts are significant for understanding script implementations in large systems.