Skip to main content
added 29 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

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&

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}')

var=$(/bin/ps r -o pid,cmd|grep "w1"| grep -v "grep"|awk '{print $1}')

That will store pidthe PIDs of all instances of w1 in var$var.
So, if I do:

echo $var

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]}

echo ${var[0]}
echo ${var[1]}

And so on (something like arrays). How tocan 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).

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 am doing this:

var=$(/bin/ps r -o pid,cmd|grep "w1"| grep -v "grep"|awk '{print $1}')

That will store pid of all instances of w1 in var.
So, if I do:

echo $var

It will print out 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 to 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).

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).

Slight review improving formalisms, capitalizing the PID word, and subtle ortography review.
Source Link

Let's say, amI'm running multiple instances of process while1.cw1 (from source code while1.c) in the background (while1.cwhile1.c contains an infinite while loop):

gcc while1.c -o w1

./w1&

./w1&

./w1&

./w1&

Now I want to get the pidPID of every instance. For that am doing this:

var=$(/bin/ps r -o pid,cmd|grep "w1"| grep -v "grep"|awk '{print $1}')

That will store pid of all instances of w1w1 in var. 
So, if iI do:

echo $var

itIt will print out pid'sPIDs of all instances of w1w1.

But I want to access individual pid's PIDs,i i.e., something like:

echo ${var[0]}

echo ${var[1]}

andAnd so on  (something like arrays). How to do that?

PS:  echo ${var[0]} is printing all pid'sPIDs while echo ${var[1]}or or echo ${var[2]} and so on, is printing nothing  (i.e. blank lines of output).

Let's say, am running multiple instances of process while1.c in 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 am doing this:

var=$(/bin/ps r -o pid,cmd|grep "w1"| grep -v "grep"|awk '{print $1}')

That will store pid of all instances of w1 in var. So, if i do

echo $var

it will print out pid's of all instances of w1.

But I want to access individual pid's ,i.e., something like:

echo ${var[0]}

echo ${var[1]}

and so on(something like arrays). How to do that?

PS:echo ${var[0]} is printing all pid's while echo ${var[1]}orecho ${var[2]} and so on, is printing nothing(i.e. blank lines of output).

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 am doing this:

var=$(/bin/ps r -o pid,cmd|grep "w1"| grep -v "grep"|awk '{print $1}')

That will store pid of all instances of w1 in var. 
So, if I do:

echo $var

It will print out 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 to 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).

edited tags
Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Source Link
Loading