Let's say I'm running multiple instances of process w1 (from source code while1.c) in the background (while1.c contains an infinite while loop):
gcc while1.c -o w1
./w1&
./w1&
./w1&
./w1&
Now I want to get the PID of every instance. For that, I am doing this:
var=$(/bin/ps r -o pid,cmd|grep "w1"| grep -v "grep"|awk '{print $1}')
That will store the PIDs of all instances of w1 in $var.
So, if I do:
echo $var
It will print out the PIDs of all instances of w1.
But I want to access individual PIDs, i.e. something like:
echo ${var[0]}
echo ${var[1]}
And so on (something like arrays). How can I do that?
PS: echo ${var[0]} is printing all PIDs while echo ${var[1]} or echo ${var[2]} and so on, is printing nothing (i.e. blank lines of output).