Solved

Passing PHP variables to shell program through shell_exec

Posted on 2013-01-07
3
1,613 Views
Last Modified: 2013-01-11
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');
0
Comment
Question by:dkauger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38753723
Try...

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

http://us3.php.net/manual/en/language.types.string.php
0
 

Author Comment

by:dkauger
ID: 38754647
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 :-)
0
 

Author Comment

by:dkauger
ID: 38769311
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question