Reputation: 69
I have been going crazy trying to figure out this syntax...can anyone point me in the right direction?
#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
The problem is where I try to assign tempAff0="$(pid"${pidCtr}"Aff)"
and when I try to do a final concatenation pid${pidCtr}Aff="$tempAff0 $tempAff1"
.
I attempted to make the change where I use arrays instead of strings, however, I am still having an issue with the fact that my array name contains a variable:
#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
The culprit is here where I want to append to the array:
pid${pidCtr}Aff[${#pid${pidCtr}Aff[*]}]="$tempAff"
Upvotes: 0
Views: 1199
Reputation: 9018
Alternate solution: instead of trying to dynamically create a variable name, replace your individual varialbes pid0Aff
, pid1Aff
, etc with an array(e.g., pidAff
), and use pidCtr to index it.
This should put you on the right track:
#! /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
Upvotes: 1