0

我一直在疯狂地试图弄清楚这种语法......谁能指出我正确的方向?

#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"
4

2 回答 2

1

替代解决方案:与其尝试动态创建变量名,不如用数组(例如,)替换您的各个变量,等pid0Aff,并使用 pidCtr 对其进行索引。pid1AffpidAff

这应该使您走上正确的轨道:

#! /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
于 2013-10-21T19:02:28.193 回答
0

使用 BASH 间接作为:

echo "${!tempAff0} ${!tempAff1}"
于 2013-10-21T19:02:11.783 回答