#!/bin/bash
# 算術測試.
# (( ... ))結構可以用來計算並測試算術表示式的結果.
# 退出狀態將會與[ ... ]結構完全相反!
(( 0 ))
echo "exit status of \"(( 0 ))\" is $?." # 1
(( 1 ))
echo "exit status of \"(( 1 ))\" is $?." # 0
(( 5 > 4 )) # 真
echo "exit status of \"(( 5 > 4 ))\" is $?." # 0
(( 5 > 9 )) # 假
echo "exit status of \"(( 5 > 9 ))\" is $?." # 1
(( 5 - 5 )) # 0
echo "exit status of \"(( 5 - 5 ))\" is $?." # 1
(( 5 / 4 )) # 除法也可以.
echo "exit status of \"(( 5 / 4 ))\" is $?." # 0
(( 1 / 2 )) # 除法的計算結果 < 1.
echo "exit status of \"(( 1 / 2 ))\" is $?." # 擷取之後的結果為 0.
# 1(( 1 / 0 )) 2>/dev/null # 除數為0, 非法計算.
# ^^^^^^^^^^^
echo "exit status of \"(( 1 / 0 ))\" is $?." # 1
# "2>/dev/null"起了什麼作用?
# 如果這句被刪除會怎樣?
# 嘗試刪除這句, 然後在執行這個指令碼.
exit 0
********************==
[email protected] ~/yang # ./calucate.sh
exit status of "(( 0 ))" is 1.
exit status of "(( 1 ))" is 0.
exit status of "(( 5 > 4 ))" is 0.
exit status of "(( 5 > 9 ))" is 1.
exit status of "(( 5 - 5 ))" is 1.
exit status of "(( 5 / 4 ))" is 0.
exit status of "(( 1 / 2 ))" is 1.
exit status of "(( 1 / 0 ))" is 1.
[email protected] ~/yang #
shell指令碼算術運算子使用詳解
shell中常用的算術運算子如下所示。對兩個變數做加法。對兩個變數做減法。對兩個變數做乘法。對兩個變數做除法。對兩個變數做冪運算。取模運算,第乙個變數除以第二個變數求餘數。加等於,在自身基礎上加第二個變數。減等於,在第乙個變數的基礎上減去第二個變數。乘等於,在第乙個變數的基礎上乘以第二個變數。除等於...
shell指令碼中的算術運算
本文將介紹6種shell指令碼中算術運算的方法,各有優缺點,當然了,最常用的就那兩種,我會特別指出。6種算術運算方法是 let operation expr operation operation operation 用awk 做算術運算 echo operation bc let no1 let ...
Shell指令碼中使用test測試命令測試數值
test 101 le 99 101是否小於或等於99 類似的特殊符號還有 eq 判斷是否相等 ge 判斷是否大於或等於 lt 判斷是否小於 ne 判斷是否不等於 可以使用命令 代替test命令來作為邏輯表示式 bin bash echo if 101 smaller than 100 if tes...