我一直在瘋狂試圖找出這種語法...任何人都可以指出我的方向正確嗎?BASH-字符串賦值,包含一個變量的字符串名稱
#Counter to iterate through the ENTIRE affinity array
affArrayCtr=0
#Counter to go through 4 cores for EACH pid
affCtr=0
#Counter to go through each PID to keep track of which PID we are on
pidCtr=0
#Affinity array size
affArraySz=${#affConfigArray[@]}
#Each core's new affinity, strings initialized to empty
pid0Aff=
pid1Aff=
pid2Aff=
pid3Aff=
while [ $affArrayCtr -lt $affArraySz ]; do
while [ $affCtr -lt $NUMCORES ]; do
if [[ ${affConfigArray[$affArrayCtr]} -eq 1 ]]; then
tempAff0="$(pid"${pidCtr}"Aff)"
tempAff1=$(($affArrayCtr % 4))
pid${pidCtr}Aff="$tempAff0 $tempAff1"
affCtr=$(($affCtr + 1))
affArrayCtr=$(($affArrayCtr + 1))
fi
affArrayCtr=$(($affArrayCtr + 1))
affCtr=$(($affCtr + 1))
done
affCtr=0
pidCtr=$(($pidCtr + $NUMCORES))
done
echo "Final affinity strings before adding the commas"
echo $pid0Aff
的問題是,我嘗試分配tempAff0="$(pid"${pidCtr}"Aff)"
,當我嘗試做最後的拼接pid${pidCtr}Aff="$tempAff0 $tempAff1"
。
我試圖使這裏我使用數組而不是字符串的變化,但是,我仍然有我的數組名稱中包含變量實際上是一個問題:
#Each core's new affinity, arrays initialized to empty
declare -a pid0Aff
declare -a pid1Aff
declare -a pid2Aff
declare -a pid3Aff
while [ $affArrayCtr -lt $affArraySz ]; do
affCtr=0
while [ $affCtr -lt $NUMCORES ]; do
if [[ ${affConfigArray[$affArrayCtr]} -eq 1 ]]; then
tempAff=$(($affArrayCtr % 4))
pid${pidCtr}Aff[${#pid${pidCtr}Aff[*]}]="$tempAff"
affCtr=$(($affCtr + 1))
affArrayCtr=$(($affArrayCtr + 1))
fi
罪魁禍首是在這裏我在哪裏要追加到數組:
pid${pidCtr}Aff[${#pid${pidCtr}Aff[*]}]="$tempAff"
所以我嘗試這個解決方案,和我仍然有我的數組名具有可變的問題。我聲明一個數組,並在這裏有一個問題: –
@calvarado請參閱編輯版本。 – CPerkins