spec36 Posted December 6, 2012 Share Posted December 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/ Share on other sites More sharing options...
Christian F. Posted December 6, 2012 Share Posted December 6, 2012 (edited) 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 December 6, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397781 Share on other sites More sharing options...
spec36 Posted December 6, 2012 Author Share Posted December 6, 2012 Thanks for the response Christian. I have tried to concatenate the variable, but it does not work example: $cmd = shell_exec('cat data.txt | grep -i'.$year.' -B 1 -A 25'); Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397783 Share on other sites More sharing options...
MDCode Posted December 6, 2012 Share Posted December 6, 2012 Variables don't parse within single quotes in PHP. End the string and concatenate the variable to it, or use sprintf (). Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397784 Share on other sites More sharing options...
Pikachu2000 Posted December 6, 2012 Share Posted December 6, 2012 Variables don't parse within single quotes in PHP. End the string and concatenate the variable to it, or use sprintf (). SocialCloud, the second example does not have the variable in single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397785 Share on other sites More sharing options...
Christian F. Posted December 6, 2012 Share Posted December 6, 2012 Spec: Any particular reason why you changed the syntax of the grep command when adding the variable? Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397786 Share on other sites More sharing options...
spec36 Posted December 6, 2012 Author Share Posted December 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397787 Share on other sites More sharing options...
jazzman1 Posted December 6, 2012 Share Posted December 6, 2012 (edited) Grep is an unix shell command. Do you know how to use it? Do you know what does mean this part of code? cat file.txt | grep -i "/$variable" Edited December 6, 2012 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397824 Share on other sites More sharing options...
kicken Posted December 6, 2012 Share Posted December 6, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397841 Share on other sites More sharing options...
jazzman1 Posted December 6, 2012 Share Posted December 6, 2012 $cmd = shell_exec('cat data.txt | grep -i'.$year.' -B 1 -A 25'); This syntax of that code is completely wrong in bash! cat file.txt | grep -i "/$variable" That syntax is correct, but do you know what does mean? Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397844 Share on other sites More sharing options...
jazzman1 Posted December 6, 2012 Share Posted December 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1397847 Share on other sites More sharing options...
spec36 Posted December 7, 2012 Author Share Posted December 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271660-shell_exec-and-variables/#findComment-1398031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.