Jump to content

Shell_Exec And Variables


spec36

Recommended Posts

I am playing around with shell_exec and trying to insert a variable into a shell_exec grep, but it is not working.

 

Here is the code that works without the variable

$cmd = shell_exec('cat file.txt | grep -i "/11" -B 1 -A 25');

 

I am trying to do something like this. I have tried multiple different concatenation attempts, but all to no avail.

 

$cmd = shell_exec('cat file.txt | grep -i "/$variable" -B 1 -A 25');

 

Any help on the proper concatenation or method to make this work is greatly appreciated.

 

Thanks.

 

--spec36

Link to comment
Share on other sites

Variables don't parse within single quotes in PHP. End the string and concatenate the variable to it, or use sprintf ().

 

PS: Remember to validate that the variable only contains numbers, or at the very least run it through escapeshellarg () before adding it to the command.

Edited by Christian F.
Link to comment
Share on other sites

I changed the syntax, because I am trying to make it work. I want what is in the variable to be part of the grep command. If you are referring to the change from file.txt to data.txt that was an error, but it doesn't matter what the file name is anyways for the sake of the example.

Link to comment
Share on other sites

$cmd = shell_exec('cat data.txt | grep -i'.$year.' -B 1 -A 25');

 

That would give you a command of (assuming $year is set to 11)

cat data.txt | grep -i11 -B 1 -A 25

 

which is incorrect.  You need a space between the -i and the variable contents.  

 

Link to comment
Share on other sites

If you want to grab all data from the data.txt that contents "/11" writing it into a new file named: data.log,it could be something like this:

 

$str = 11;
$cmd = shell_exec("cat data.txt | grep -i /$str > /home/jazzman/data.log");

 

You can also use RegEx to filter out your outputing.

$str could be a date php function grabing the name of the current month or what ever you want.

It doesn't matter what language you want to use excecuting that script , you must follow the rules of a bash/shell syntax.

Link to comment
Share on other sites

Thanks for the responses guys and gals. I figured it out. I had the command in a function, the $year variable was outside the function. Hence, my wacky results. I did not give the function the $year variable as input. Smack me for not posting all the code.

 

Works like a charm now.

 

Thanks,

 

--spec36

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.