我有以下 bash 腳本:
#!/bin/bash
# login x times and calculate avg login time, take note of slowest time and # of logins > 1s
function login() {
startTime=$(date %s)
curl --location --request GET 'http://www.example.com'
endTime=$(date %s)
local callDuration=$(expr $endTime - $startTime)
echo "$callDuration"
}
numLogins=5
allCallDurations=()
echo "starting logins"
for i in $(seq $numLogins)
do
modu=$(expr $i % 20)
if [ $modu -eq "0" ]; then
echo "20 more calls were made"
fi
duration=$(login)
allCallDurations =($duration)
done
avgDuration=$(expr $allCallDuration / $numLogins)
slowest=${allCallDurations[0]}
numSlowLogins=0
for i in $(seq $numLogins)
do
if (( slowest > ${allCallDurations[$i]} )); then
slowest=${allCallDurations[$i]}
fi
if (( ${allCallDurations[$i]} > 1 )); then
numSlowLogins=$(expr $numSlowLogins 1)
fi
done
echo "finished:"
echo "average call duration (s): $avgDuration"
echo "slowest call (s): $slowest"
echo "# calls > 1 second: $numSlowLogins"
這個想法是它用于curl對網站進行n多次 HTTP 呼叫(這里我使用 example.com,但實際上我正在對我的 Web 服務的登錄 URL 進行 RESTful 呼叫)。它會計算平均通話時長、最慢的通話以及需要超過一秒才能回傳的通話次數。
當我通過Shell Check運行這個腳本時,它說一切都很好。
但是當我運行bash myscript.sh(這是我的 Mac OS 檔案系統上這個腳本的名稱)時,我得到:
bash myscript.sh
starting logins
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 772 100 704 100 68 3171 306 --:--:-- --:--:-- --:--:-- 3477
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 772 100 704 100 68 3142 303 --:--:-- --:--:-- --:--:-- 3446
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 772 100 704 100 68 3214 310 --:--:-- --:--:-- --:--:-- 3509
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 772 100 704 100 68 3142 303 --:--:-- --:--:-- --:--:-- 3446
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 772 100 704 100 68 3320 320 --:--:-- --:--:-- --:--:-- 3641
expr: syntax error
myscript.sh: line 45: {"json":"coming-back-from-server"}0")
myscript.sh: line 49: {"json":"coming-back-from-server"}0 > 1 ")
myscript.sh: line 45: {"json":"coming-back-from-server"}0")
myscript.sh: line 49: {"json":"coming-back-from-server"}1 > 1 : syntax error: operand expected (error token is "{"json":"coming-back-from-server"}1 > 1 ")
myscript.sh: line 45: {"json":"coming-back-from-server"}0")
myscript.sh: line 49: {"json":"coming-back-from-server"}0 > 1 ")
myscript.sh: line 45: {"json":"coming-back-from-server"}0")
myscript.sh: line 49: {"json":"coming-back-from-server"}0 > 1 ")
myscript.sh: line 45: {"json":"coming-back-from-server"}0")
myscript.sh: line 49: > 1 : syntax error: operand expected (error token is "> 1 ")
finished:
average call duration (s):
slowest call (s): {"json":"coming-back-from-server"}0
# calls > 1 second: 0
where{"json":"coming-back-from-server"}只是每個 curl 執行回傳的實際json 的占位符,但重要的是要注意它實際上是為所有這些列印 JSON 值。
誰能發現我哪里出錯了,為什么它失敗了,而 Shell Check 說它很好(我的意思是 SC 有一些警告但沒有錯誤)?
uj5u.com熱心網友回復:
正如 Charles Duffy 指出的那樣,shellcheck無法診斷運行時錯誤/問題。
在這種情況下,allCallDurations[]陣列似乎是空的,這與程式中的拼寫錯誤有關,請考慮以下拼寫:
allCallDurations=() # array name ends in 's'
allCallDuration =(duration) # missing 's' on end of array name
${allCallDurations[x]} # array name ends in 's'; multiple references
您實際上正在構建一個與其余代碼(- trailing )中參考的陣列不同的陣列( allCallDuration- no trailing )。sallCallDurationss
所以,修正一個錯字,然后看看會發生什么:
# replace:
allCallDuration =(duration)
# with:
allCallDurations =(duration)
^-----
注意:我不希望shellcheck標記此問題,因為從技術上講,您可以擁有 2 個名稱不同的陣列,即使差異是單個尾隨s
好的,所以我可以猜測接下來的幾個問題可能是什么......
以下有什么區別:
allCallDurations =(duration) # previously suggested edit
allCallDurations =($duration) # additional edit?
你認為login()應該回傳什么來電?如果您login()從命令提示符呼叫會發生什么,即它是否回傳您所期望的?
uj5u.com熱心網友回復:
在
if (( slowest > ${allCallDurations[$i]} )); then
...在我們知道在運行時${allCallDurations[$i]}擴展為" "而不是數字之前,沒有什么是無效的語法。靜態檢查是檢測不到的。
除此之外:expr不應該在現代代碼中使用。用于$(( ))整數運算或bc浮點運算。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521145.html
標籤:重击壳
上一篇:計算文本中的特定模式
