Link to home
Start Free TrialLog in
Avatar of dkauger
dkauger

asked on

Passing PHP variables to shell program through shell_exec

I need to convert multiple ksh based .cgi files to php.  The shell_exec function appears to be just what I'm looking for since I need to utilize the standard shell tools like grep and awk.
It seems to work for me if I hard code what I'm looking for but doesn't recognize a variable passed from the PHP script:

This is a piece of script provided by liveaspankaj last year.  It has been modified to my needs.  The major issue is the $memberid variable is not recognized by grep.  Hardcoding the memberid works.  Quotes or no quotes are irrelevent.  I need to know how to enter the variable name so grep ( or any other program ) called by shell_exec can recognize it.
Also, pipeing ithe results to awk like this results in the entire record being displayed instead of the first field.  If I can get these two issues resolved, it will be a big step in getting all my files re-written and  i would be extremely grateful.


$output = shell_exec('grep "$memberid" MembersOnly/members.csv | awk -F, "{print $1}" | head -1');
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkauger
dkauger

ASKER

Very close!  The correct quoting solved the variable passing problem.  Piping it to awk needed one correction.  Double quotes didn't work but changing it to single quotes made it work like a charm.  Didn't need the "tail -1" from the previous solution so eliminated it.
This is what worked for me:

$output = shell_exec("grep $memberid MembersOnly/members.csv | awk -F, '{print $1}' ");

I appreciate the help and hopefully I can take it from here.  The rest will be learning PHP :-)
Avatar of dkauger

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for dkauger's comment #a38754647

for the following reason:

The submitted solution was only partially correct.