0

I'm trying to get the number of lines that was printed from the ps command, and save to a variable.

COUNT_PS= ps -C $NAME | wc -l)

the line above prints me 2, but COUNT_PS unfortunately still equals 0 (at the start of the script I've assigned COUNT_PS as 0).

It actually prints the value - but doesn't save it to COUNT_PS.

What am I doing wrong?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Gilad Neiger
  • 643
  • 1
  • 6
  • 15
  • `var=$(somecommand)` -- as it is, you're setting an empty variable named `COUNT_PS` in the environment while running `ps`. – Charles Duffy Mar 05 '19 at 18:23
  • That said, the code in question is not good practice. If you have `pgrep`, better to use it, but better than *that* is to use proper lockfiles (as with [`flock`](https://linux.die.net/man/1/flock)) / service management (systemd or launchd or whatever your OS vendor provides) and not to try to grep the process table at all. – Charles Duffy Mar 05 '19 at 18:25

0 Answers0