Solved

Passing PHP variables to shell program through shell_exec

Posted on 2013-01-07
3
1,445 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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now