0

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

#!/bin/bash
a= ps | awk '{print $1}'
echo select $a

So I am getting this output

PID
1838
1839
1840
13226
select 

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.

2
  • You have basic syntax errors for what you want to achieve. See e.g. stackoverflow.com/q/16072351/445621 for an example of command substitution into a shell variable. Apr 9, 2014 at 17:05
  • Thanks @DanielAndersson Using this way I am not getting select and actually I want ','(comma) also in middle of them. And the pid is coming how can I restrict that ?
    – temp
    Apr 9, 2014 at 17:16

1 Answer 1

1

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.

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

Output

$ ./printpid.sh
select 2293836, 3276802, 6422606, 6750318, 7667882, 7798894, 8716412
5
  • @temp, You're welcome. I hope you're copy-pasting this and can backspace the last comma. Removing that one would be way harder for me!
    – Wally
    Apr 9, 2014 at 17:18
  • Sorry @wally but can't we manipulate ',' because while auditing I am getting errors.
    – temp
    Apr 9, 2014 at 17:27
  • @temp, I don't understand. You want the comma at the end? You want a space after each comma? The space is easy. Just add a space after the %s, in the code.
    – Wally
    Apr 9, 2014 at 17:37
  • my apologies if i am bugging you I go not want the comma(only one comma) at the end of the statment
    – temp
    Apr 9, 2014 at 17:56
  • @temp. Nope! Not bugging me. Here's a version without the last comma. And I added the spaces too.
    – Wally
    Apr 9, 2014 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .