Jump to content

Proper Command Line usage


Sonzai

Recommended Posts

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.

   

Link to comment
https://forums.phpfreaks.com/topic/282769-proper-command-line-usage/
Share on other sites

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.