How to print process id into statment

Posted on

Problem :

I have assigned a work to take take all process and make them into a statement. So I tried this script.

a= ps | awk '{print $1}'
echo select $a

So I am getting this output


I know that a variable is taking many arguments and unable to insert into one, but i am unable to get the logic.

I want a output similar to this.

select 1838, 1839, 1840, 13226

Thanks in advance.

Solution :

Updated answer

Use backticks “ or $( ) to wrap the commands whose output will be saved as the variable.

Tail +2 to show all but the first line.

Awk adds the comma and spaces between the valuese

Sed removes the comma-space at the end of the line.

a=$(ps | tail +2 | awk '{printf ("%s, ",$1)}' | sed "s/, $//")
echo select $a


$ ./
select 2293836, 3276802, 6422606, 6750318, 7667882, 7798894, 8716412

Leave a Reply

Your email address will not be published. Required fields are marked *