在shell script計算加減乘除的方式, 可以用expr
#!/bin/sh
VALUE=100
VALUE=`expr $VALUE - 10`
echo "After minus: $VALUE"
VALUE=`expr $VALUE + 10`
echo "After plus: $VALUE"
VALUE=`expr $VALUE / 10`
echo "After divide: $VALUE"
VALUE=`expr $VALUE * 10`
echo "After multiply: $VALUE"
執行結果:
After minus: 90
After plus: 100
After divide: 10
After multiply: 100
不過在Android上, 會發生找不到expr的錯誤
所以要改用下面的方式
#!/bin/sh
VALUE=100
VALUE=$((VALUE - 10))
echo "$VALUE after minus"
VALUE=$((VALUE + 10))
echo "$VALUE after plus"
VALUE=$((VALUE / 10))
echo "$VALUE after divide"
VALUE=$((VALUE * 10))
echo "$VALUE after multiply"
執行結果:
90 after minus
100 after plus
10 after divide
100 after multiply