0

NOTE FROM OP: Oops. My mistake. I happened to let grep hunt for something(s) non-existent. Of course I got no output. And yes, this is a dup of another question.

<><><><><><><><><><><><><><><><><><><><>

There are many answers on the web to (most of) this question. The "most of" part is my problem.

How do I capture the output of a command line into a bash array when the command line contains pipe chars, "|"?

 array=($(ps -ef | grep myproc | grep -v grep))

doesn't work. Neither does:

 array=(`ps -ef | grep myproc | grep -v grep`)

(those are backquotes in case your font mangles them).

And, can the given answer be use with array+= syntax?

Community
  • 1
  • 1
Wes Miller
  • 2,111
  • 34
  • 62
  • 1
    Possible duplicate of this question: http://stackoverflow.com/questions/1753366/redirect-output-to-a-bash-array – seliopou Dec 27 '12 at 21:08
  • What do you mean by _it doesn't work_? for me it works, I mean, I have no syntax error. – gniourf_gniourf Dec 27 '12 at 21:12
  • Well, it helps if you choose to pipe to grep for process names that actually exist. Mea culpa. Thanks fo all for the quick answers. – Wes Miller Dec 27 '12 at 21:28
  • and where is a pipe character coming from in the output of `ps`? Better to edit in some intermediate results from actual runs so we can see what's going on. Good luck. – shellter Dec 27 '12 at 21:29
  • instead of `grep myproc | grep -v grep` if you hit: `grep m[y]proc`, the regex won't match itself! – F. Hauri Dec 27 '12 at 22:37

1 Answers1

1
array=($(ps -ef | grep myproc | grep -v grep))

works perfectly well. You can check it when you show the number of elements in your array

echo ${#array[*]}

or the complete array with

echo ${array[*]}
Olaf Dietsche
  • 69,448
  • 7
  • 95
  • 188