I'm really struggling with getting some dynamically assigned variables working, even though I've been through the rather wonderfully informative SO article that I've been staring at for a couple of hours now ... I just can't seem to get anything working. I'm using Bash 4.4.20 on Ubuntu 18.04.5.
I have a function that sets up some variables, much like:
declare -g AthreadCount
AthreadCount=$(ps -ef | grep svcA) # 2
declare -g BthreadCount
BthreadCount=$(ps -ef | grep svcB) # 4
declare -g CthreadCount
CthreadCount=$(ps -ef | grep svcC) # 1
In another function, I have the array set up of those services:
declare -a -g services=(A B C)
(to be fair I'm parsing a jq
result for the declaration of the array, but it is working and globally available in other functions therefore I'm comfortable it's working as expected)
In another function, I want to evaluate each variables' value and I can't get - what I understand to be called "pointer" assignments - working. I believe my code should look like this:
for sts in "${services[@]}"; do
echo ${sts}
svc="${sts}threadCount"
echo ${!svc}
done
And I would expect:
A
2
B
4
C
1
but I end up getting
A
B
C
Obviously it's not working and I've gone through what I believe to be everything I can do in order to get it working.
Thoughts / comments?!