Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Gilles Aug 8 '14 at 0:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.