Sonzai Posted October 6, 2013 Share Posted October 6, 2013 Hello! So I am back to using PHP...and back to having questions about it I am not understanding how to execute a php script from the command line and properly pass variables. the syntax is php -f <file> [args] I understand, but I have been receiving the error below when I try to execute the following test code. launcher.php: <?php $argv[1] = "first argument"; $argv[2] = "second argument"; $simple ="simple"; exec(php -f launched.php simple); ?> launched.php <?php $stdout = fopen('php://stdout', 'w'); fwrite($stdout, $argv[1]); fwrite(STDOUT, $argv[1]); ?> ~ The error I receive when running: php launcher.php is the following- Parse error: syntax error, unexpected T_STRING in /var/www/html/Project2/launcher.php on line 7 *please note in launcher, I have tried passing simple as you see it, as well as by $simple and "simple" *I am running Apache Web Server environment with PHP CLI configuration. Quote Link to comment Share on other sites More sharing options...
Solution .josh Posted October 7, 2013 Solution Share Posted October 7, 2013 exec expects a string argument. wrap it in quotes. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 7, 2013 Share Posted October 7, 2013 p.s. - are you sure you even need to run that through exec? If you include or require it, it will parse the code in launched.php when you run launcher.php. And you don't need to pass arguments to it; just set and look for vars like normal. There is rarely ever a reason to actually use exec for anything; one of those things you should avoid if possible. Quote Link to comment Share on other sites More sharing options...
Sonzai Posted October 7, 2013 Author Share Posted October 7, 2013 Aha! This project is making me miss simple things... Thanks .josh - and I think I do need to use exec because though the above is just a test, I will be using the real script to run C programs...but if you have any input on this I'd take it. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 7, 2013 Share Posted October 7, 2013 ah okay, i guess you will need to use exec for that. Just be really careful if you intend to pass values through exec that come from users or other sources users can affect. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted October 7, 2013 Share Posted October 7, 2013 (edited) Aha! This project is making me miss simple things... Thanks .josh - and I think I do need to use exec because though the above is just a test, I will be using the real script to run C programs...but if you have any input on this I'd take it. PHP provides(for better or for worse) a bunch of numbers of predefined variables. So, in this example you might be consider using $argc and $argv. Take a look at examples, I'm using a shell_exec() function instead exec(). launcher.php: <?php $argv[1] = "first argument"; $argv[2] = "second argument"; $simple = "simple"; echo shell_exec("php -f launched.php $argv[1] $argv[2] $simple"); launched.php var_dump($argc); // The number of arguments passed to script var_dump($argv); // Array of arguments passed to script As you can see the output above, in $argv[1] and $argv[2] the value is being treated by the shell command with two different arguments, b/s of the blank spaces between the words. You could create some custom function to escape this empty space with "\ " symbol. That's off the top of my head: $argv[1] = "first argument"; echo shell_exec("php -f launched.php ".EscapeEmptySequence($argv[1])); function EscapeEmptySequence($str){ $search = ' '; $replace = "\ "; return str_replace($search, $replace, $str); } Personally, I rarely use php to execute commands in the shell and prefer BASH instead! Edited October 7, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
kicken Posted October 7, 2013 Share Posted October 7, 2013 You could create some custom function to escape this empty space with "\ " symbol. That's off the top of my head: PHP has a function for that: escapeshellarg Quote Link to comment Share on other sites More sharing options...
.josh Posted October 7, 2013 Share Posted October 7, 2013 well spaces aside, escapeshellarg and/or escapeshellcmd should be used for making sure someone doesn't try to pass arbitrary commands Quote Link to comment 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.