My expectation of the following code is that I'll have two arrays of shared memory ID's. One that lists the ID's with zero attachments, and one that lists the ID's with more than zero attachments.
The command ouput that I'm parsing is:
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x6c020df2 131072 monitor 600 4276656 6
0x77020d34 4620289 usera 666 104 0
0x78020d34 4653058 usera 666 651328000 0
0x72020d34 4685827 usera 666 15808 0
And the code is as follows:
free_shmids=()
used_shmids=()
IFS=$'\n'
for line in $(sudo ipcs -m | grep '0x'); do
nattch=$(echo $line | awk '{ print $NF; }')
shmid=$(echo $line | awk '{ print $2; }')
if [ $nattch -eq "0" ]; then
free_shmids+=($shmid)
else
used_shmids+=($shmid)
fi
done
unset -v IFS
Instead, the list of free ID's is just the first free ID of the three available. I've only got one used shared memory segment here, so I assume that's failing the same way even though I can't prove it.
For reference, I'm running bash 4.3.11, so I'm pretty sure I've got access to the +=
syntax for arrays.
EDIT:
To iterate the array of free ID's, I'm doing this:
for id in $free_shmids; do
echo $id
done
I intend to do something more complex in the body of the for loop, of course, but that exact method of iteration doesn't list every element of the array.