michael.davis Posted February 10, 2012 Share Posted February 10, 2012 Good Friday to everyone! I am writing some PHP code to find certain files and the search the files for a certain string. For example, this is what I have. This works in a LINUX shell environment, yet I need to have it work in PHP since the GUI is web based. Can anyone help? Thanks! Mike <?php $output = shell_exec('find /sync/www/html/ohx/products/MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IN EFFECT" /dev/null {} \;'); echo "<pre>$output</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/ Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 shell_exec will always execute on the server, regardless of how you access the file (through the cli or through a web server). Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316589 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 Thank you for the reply. I understand that. Not sure what I am missing to make this work under PHP. Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316602 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 This is already PHP code. What's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316609 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 The shell_exec code is not working. Is there something that I am missing? Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316612 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 This is working for me: $output = shell_exec('find ~/Desktop/ -type f -exec grep "secure" /dev/null {} \;'); You should be able to simply copy and paste your command from inside shell_exec directly into a command line, and vice versa. Does it work if you try it directly? Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316616 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 I am not able to display anything. I believe it has something to do with the wildcard (*) and the multiwords in the grep. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316620 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 Oh, you're not using find properly. Find takes two arguments: find <where to look> <what to look for> You're looking for something more like: find /sync/www/html/ohx/products/ -name MEMNPWOHX* Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316623 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 That was it! Thank you, kind Sir! Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316624 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 This works well. Thank you for your help. Trying to get the variables to work is being a pain. $output = shell_exec('find "$pil" -type f -exec grep "$string" /dev/null {} \;'); Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316646 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 PHP variables aren't interpolated inside single quotes, you have to concatenate them. You should also run them through shell_escape. Also, remember that $pil is in the wrong format, you need <location> and <pattern> out of that. Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316649 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 Ok here is what I came up with, Thanks for the huge help from ManiacDan! Now I have a list of files that should have say HEAT ADVISORY in them, but when I run the script from the form to gather the $pil and $string, it only shows the listing from one file. Not all of them. MEMNPWOHX*HEAT ADVISORY IS IN EFFECTfind MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IS IN EFFECT" /dev/null {} \; MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PART OF MIDDLE TN MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PART OF MIDDLE TN MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PART OF MIDDLE TN <?php session_start(); $pil = $_POST['pil']; $pil = strtoupper($pil); $string = $_POST['string']; $string = strtoupper($string); $pil = "$pil"."*"; echo $pil; echo $string; $cmd = "find $pil -type f -exec grep \"$string\" /dev/null {} \;"; echo $cmd; $output = shell_exec($cmd); echo "<pre>$output</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316664 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 This is what I get from the command line in LINUX. As you will see, I only get one file listed with the php code.... and all of them in a LINUX shell. MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 8 PM CDT MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM CDT THIS AFTERNOON TO 8 PM CDT MEMNPWOHX.0610:...A HEAT ADVISORY IN EFFECT FOR THE WESTERN QUARTER OF MIDDLE TN MEMNPWOHX.0610:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING... MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON SUNDAY TO 10 PM CDT MONDAY... MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 10 PM CDT MONDAY... MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM THIS AFTERNOON TO 10 PM CDT MONDAY... MEMNPWOHX.0706:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 7 PM CDT MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT FRIDAY... MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON MONDAY TO 7 PM CDT TUESDAY... MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT TUESDAY... MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY FOR MIDDLE MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY... MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY... MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY... MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY... MEMNPWOHX.0808:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT THIS AFTERNOON... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 2 PM THIS AFTERNOON TO 7 PM CDT MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FOR THE FAR NORTHWEST PORTION OF MIDDLE MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT WEDNESDAY... MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY... MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM 7 PM THIS EVENING TO 7 PM CDT Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316667 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 You ran this command on the command line? find MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IS IN EFFECT" /dev/null {} \; And that command produced the list in your last post? That command is not correctly formatted. Also, you absolutely must run these strings through escapeshellarg (which automatically quotes them for you). Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316684 Share on other sites More sharing options...
michael.davis Posted February 10, 2012 Author Share Posted February 10, 2012 Yes that worked. When I run the command in a terminal shell like you listed, this is what I get: [mikey@ohx-lw-ostrich products]# find /sync/www/html/ohx/products/ -name MEMNPWOHX* -exec grep "HEAT ADVISORY IN EFFECT" find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] [mike@ohx-lw-ostrich products]# find MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IN EFFECT" /dev/null {} \; MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 8 PM CDT MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM CDT THIS AFTERNOON TO 8 PM CDT MEMNPWOHX.0610:...A HEAT ADVISORY IN EFFECT FOR THE WESTERN QUARTER OF MIDDLE TN MEMNPWOHX.0610:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING... MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON SUNDAY TO 10 PM CDT MONDAY... MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 10 PM CDT MONDAY... MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM THIS AFTERNOON TO 10 PM CDT MONDAY... MEMNPWOHX.0706:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 7 PM CDT MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT FRIDAY... MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON MONDAY TO 7 PM CDT TUESDAY... MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT TUESDAY... MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY FOR MIDDLE MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY... MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY... MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY... MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY... MEMNPWOHX.0808:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT THIS AFTERNOON... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 2 PM THIS AFTERNOON TO 7 PM CDT MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FOR THE FAR NORTHWEST PORTION OF MIDDLE MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY... MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT WEDNESDAY... MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY... MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM 7 PM THIS EVENING TO 7 PM CDT Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316701 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 The PHP script is not in this folder, is it? You're running a find command (incorrectly, still) inside a folder, and it's only working because `find` will return all the files in the current folder if you use it wrong. Since the PHP file is NOT in this folder, when it tries to run `find` it will behave wrong. Quote Link to comment https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316718 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.