我一直在疯狂地试图找出这种语法......有人能指出我正确的方向吗?
#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"
答案 0 :(得分:1)
替代解决方案:不是尝试动态创建变量名称,而是使用数组(例如pid0Aff
)替换您的各个变量pid1Aff
,pidAff
等,并使用pidCtr索引它。
这应该让你走上正轨:
#! /bin/bash
pidAff[0]='zero'
pidAff[1]='one'
pidAff[2]='two'
pidAff[3]='three'
pidAffArrayCtr=0
pidAffArraySz=4
while [ $pidAffArrayCtr -lt $pidAffArraySz ]; do
echo "pidAff of $pidAffArrayCtr is ${pidAff[$pidAffArrayCtr]}"
pidAffArrayCtr=$(($pidAffArrayCtr + 1))
done
答案 1 :(得分:0)
将BASH间接使用为:
echo "${!tempAff0} ${!tempAff1}"