在這個 shell 腳本中更新我的全域變數時遇到問題。我讀過回圈內的變數在子外殼上運行。有人可以澄清這是如何作業的以及我應該采取哪些步驟。
USER_NonRecursiveSum=0.0
while [ $lineCount -le $(($USER_Num)) ]
do
thisTime="$STimeDuration.$NTimeDuration"
USER_NonRecursiveSum=`echo "$USER_NonRecursiveSum $thisTime" | bc`
done
uj5u.com熱心網友回復:
這種特定的回圈樣式不會在子 shell 中運行,它會很好地更新變數。您可以在以下代碼中看到,除了添加問題中未包含的內容外,與您的代碼相同:
USER_NonRecursiveSum=0
((USER_Num = 4)) # Add this to set loop limit.
((lineCount = 1)) # Add this to set loop control variable initial value.
while [ $lineCount -le $(($USER_Num)) ]
do
thisTime="1.2" # Modify this to provide specific thing to add.
USER_NonRecursiveSum=`echo "$USER_NonRecursiveSum $thisTime" | bc`
(( lineCount = 1)) # Add this to limit loop.
done
echo ${USER_NonRecursiveSum} # Add this so we can see the final value.
該回圈運行四次,1.2每次都將值添加到從零開始的值,您可以看到它4.8在回圈完成后結束。
雖然該echo命令確實在子 shell 中運行,但這不是問題,因為反引號會顯式捕獲它的輸出并將其“傳遞”到當前 shell。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471512.html
