I'm working on a PHP script in which I have to run shell script,
I have 2 option:
If I'm able to send php variable value to shell
Or I can write shell directly in PHP
I used
shell_exec(dirname(__FILE__) ."/shl.sh");
for execute shell .... Now the problem is .. if i use #!/usr/bin/php in shell it only resolve code within <?php ?> and print shell statement directly on screen.
The best option (by far!) would be to modify the external script to accept command line parameters.
So instead of
shell_exec('sh.sh');
where all variables are embedded, make it into
shell_exec("./sh.sh $opt1 $opt2");
where you can pass your variables easily.
An example for a bash script to use these arguments would be :
#!/bin/bash
echo "My $1 will kick your $2 anytime"
which will replace $1 with the first argument, and $2 with the second.
Related
When I use web browser I pass parameters by this way:
http://localhost/script.php?nr=444524
and get it this way:
$var = $_GET('nr');
print_r($var);
but how to achieve the same result (pass and get parameters) when I compile same script with cmd on windows?
c:\php.exe script.php ?nr=444524
this way doesn't work
It doesn't work that way. $_GET is a variable created to feed in data from an HTTP request.
On the command line you enter arguments as:
php script.php 444524
From here you can ready the arguments as print_r($argv);.
All the words you put into the command line, starting with the script name can be found in the global variable $argv. Launch your script with various parameters and check the output of print_r($argv); to see what you get.
Check the documentation here
I am near losing my mind cause of a perl script I want to call via PHP.
I have a PHP Form where I put in a MySQL Query which gets stored in a file and choosing some variables.
If I call any SHELL command like "top" ... everything works fine, but as soon as I try to call my perl script with variables, there are no results at all.
The file where the results should get stored stays empty.
That's the calling part from the PHP File:
if (isset($_POST['submit'])) {
$file = 'query.sql';
$query = $_POST['query'];
file_put_contents($file, $query);
$command = "perl /home/www/host/html/cgi/remote-board-exec.pl -sqlfileexec query.sql > /home/www/host/html/cgi/passthrutest.txt";
exec($command, &$ausgabe, $return_var);
There is no error message and i already tried debug things, but nothing helped :(
Are you sure that perl is being executed? Perhaps you ought to replace the command with 'which perl' just to make sure, and to use the full path to Perl. Another idea is to make sure your:
perl script is executable (use chmod)
ensure it has '#!/usr/bin/perl' (or wherever your path to perl is)
change the command to "/home/www/host/cgi/remote-board-exec.pl..." without the perl command
dump the contents of your output array ($ausgabe) as if the command fails to execute you may find out what is happening.
I am creating PHP script which is taking arguments from command line right now but after sometime it may change to simply including my PHP script.
How can I prepare my script for both scenarios?
Can I create such PHP script so that it can take argument from command line and from other script which is simply including my script?
let's say script A is the first script that can be called from the commnad line and included in another script and script B is the one that includes it.
if you include script A in B you'll have access to any variable in script B. so why don't you add a check in script A to see if a param has been passed from the command line, if no params have been passed from the command line use whatever variables you created in script B
If I understand well, you'll be forced to use a parameter to tell if your script is using the commandline parameter, or if he will call your other script.
In your script, you can do something like :
if (!strcmp($argv[1], "-s"))
shell_exec('php YourOtherScript.php [put_args_here]');
else
your_current_script($argv);
Then, if you want to execute your other script, you'll make a command like :
php MyScript.php -s [args]
Otherwise, simply put the standard arguments :
php MyScript.php [args]
could be as simple as:
if(isset($argv[1])){
//commadn line arguments
$foo=$argv[1]
}else{
//not
$foo=$foo
}
I am wanting to call a php file using exec.
When I call it I want to be able to pass a variable through (an id).
I can call echo exec("php /var/www/unity/src/emailer.php"); fine, but the moment I add anything like echo exec("php /var/www/unity/src/emailer.php?id=123"); the exec call fails.
How can I do this?
Your call is failing because you're using a web-style syntax (?parameter=value) with a command-line invokation. I understand what you're thinking, but it simply doesn't work.
You'll want to use $argv instead. See the PHP manual.
To see this in action, write this one-liner to a file:
<?php print_r($argv); ?>
Then invoke it from the command-line with arguments:
php -f /path/to/the/file.php firstparam secondparam
You'll see that $argv contains the name of the script itself as element zero, followed by whatever other parameters you passed in.
try echo exec("php /var/www/unity/src/emailer.php 123"); in your script then read in the commandline parameters.
If you want to pass a GET parameter to it, then it's mandatory to provide a php-cgi binary for invocation:
exec("QUERY_STRING=id=123 php-cgi /var/www/emailer.php");
But this might require more fake CGI environment variables. Hencewhy it is often advisable to rewrite the called script and let it take normal commandline arguments and read them via $_SERVER["argv"].
(You could likewise just fake the php-cgi behaviour with a normal php interpreter and above example by adding parse_str($_SERVER["QUERY_STRING"], $_GET); on top of your script.)
this adapted script shows 2 ways of passing parameters to a php script from a php exec command:
CALLING SCRIPT
<?php
$fileName = '/var/www/ztest/helloworld.php 12';
$options = 'target=13';
exec ("/usr/bin/php -f {$fileName} {$options} > /var/www/ztest/log01.txt 2>&1 &");
echo "ended the calling script";
?>
CALLED SCRIPT
<?php
echo "argv params: ";
print_r($argv);
if ($argv[1]) {echo "got the size right, wilbur! argv element 1: ".$argv[1];}
?>
dont forget to verify execution permissions and to create a log01.txt file with write permissions (your apache user will usually be www-data).
RESULT
argv params: Array
(
[0] => /var/www/ztest/helloworld.php
[1] => 12
[2] => target=13
)
got the size right, wilburargv element 1: 12
choose whatever solution you prefer for passing your parameters, all you need to do is access the argv array and retrieve them in the order that they are passed (file name is the 0 element).
tks #hakre
I know this is an old thread but it helped me solve a problem so I want to offer an expanded solution. I have a php program that is normally called through the web interface and takes a long list of parameters. I wanted to run it in the background with a cron job using shell_exec() and pass a long list of parameters to it. The parameter values change on each run.
Here is my solution: In the calling program I pass a string of parameters that look just like the string a web call would send after the ?. example: sky=blue&roses=red&sun=bright etc. In the called program I check for the existence of $argv[1] and if found I parse the string into the $_GET array. From that point forward the program reads in the parameters just as if they were passed from a web call.
Calling program code:
$pars = escapeshellarg($pars); // must use escapeshellarg()
$output = shell_exec($php_path . ' path/called_program.php ' . $pars); // $pars is the parameter string
Called program code inserted before the $_GET parameters are read:
if(isset($argv[1])){ // if being called from the shell build a $_GET array from the string passed as $argv[1]
$args = explode('&', $argv[1]); // explode the string into an array of Type=Value elements
foreach($args as $arg){
$TV = explode('=', $arg); // now explode each Type and Value into a 2 element array
$_GET[$TV[0]] = $TV[1]; // set the indexes in the $_GET array
}
}
//------------------------
// from this point on the program processes the $_GET array normally just as if it were passed from a web call.
Works great and requires minimal changes to the called program. Hope someone finds it of value.
I'm trying to set a bash environment variable using PHP (from command line) with no success.
$buff=array();
$buff[]="VARTESTKEY=VARTESTVALUE";
$buff[]="export VARTESTKEY";
file_put_contents('script.sh', implode("\n",$buff));
system('source script.sh');
I've even tried using a script to output the key value which gets evaled:
$buff=array();
$buff[]="echo VARTESTKEY=VARTESTVALUE";
file_put_contents('script.sh', implode("\n",$buff));
system('eval "$(bash script.sh)"');
But still nothing.
Any ideas? I don't mind using any other tool (perl, python, c, etc.) as long as it can do its job by being called from the PHP system function.
Do you need this environment variables before running another bash script?
You can just use putenv("KEY=VAL");
Es:
<?php
putenv("ASD=LOL");
system("echo \$ASD");
?>
Edit:
<?php
echo "VARTESTKEY=VARTESTVALUE";
?>
launch it as:
$ eval `php script.php` && echo $VARTESTKEY