0

Using this

OF=$(ps fax | grep 'php-fpm: master process' | awk '{print $1}')  
IDX=`expr index $OF ' '`

I get an error. The results of the $OF variable are:

27797 27495

What is the error here? I think it has to do with how the variable is being passed into the expression. Also, have tried putting ' quotes around the $OF variable to no avail. That just returns 0 as not found.

GBA
  • 3
  • 1
    You can change the first line to. of=$(ps fax | awk '/php-fmp: master process/{print $1}'). However, if you havepgrep` you better use pgrep. Also Explain in details what you want. – Valentin Bajrami Aug 07 '14 at 23:07

2 Answers2

0

You need double quotes:

IDX=`expr index "$OF" ' '`

Without quotes, the expr command looks like

expr index 27797 27495 ' '

which doesn't make any sense.  With single quotes, you are passing expr the three-character long string consisting of $, O, and F (which doesn't contain any spaces).  It's almost always a good idea to put variables in double quotes -- and it's essential when the value might contain spaces or any other special characters.

0

You should use pgrep to grep the Process ID of a process. This is the safest way there is. Some systems(legacy systems) don't have pgrep so you'd be forced to use something as ps. In case you use ps You should consider the following. Your line uses grep and awk which is not necessary as you could handle all that using awk.

of=$(ps fax | awk '/[p]hp-fpm: master process/{print $1}') 

As a side note. Don't use upper case for normal variable names. By convension, Environment variables are CAPITALIZED.

At this stage your variable $of will hold the process id of the php-fpm. Since your question is not clear, I'm not sure what expr is doing there.