Jump to content

Proper Command Line usage


Sonzai
Go to solution Solved by .josh,

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by jazzman1
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.