I'm trying to execute a shell command from php using the exec command
The following command works
echo exec('whoami');
but I need to execute the following shell command
cat > /var/www/myfolder/abc.txt
hi
hello
welcome
the problem is that the above shell command has to be executed in multiplelines. The words "hi", "hello", "welcome" are dynamic and come from php variables. Is there a workaround for this problem.
To execute a multiline shell command in php:
passthru("bash <<'END'
echo \"executed in new bash session\";
pwd;
whoami;
exit;
END
");
This way you could for example connect to a ssh server using php and then execute some commands there
I don't understand what you mean by cat > abc.txt. This command will empty the file.
Related
I want to execute the below command from PHP shell_script in Linux environment.
shell_exec('at 12:39 <<< "mkdir newfolder"');
I have tried in all the method of PHP to execute this script but it is not working.
In terminal when I am running at 12:39 <<< "mkdir newfolder" it is executing and the task is scheduling.But when I am trying the same script to run in php by using shell_exec it is not working.
You can understand the issues when you type at 12:39 it will show you to put the input and press Ctrl+d to complete execution. to make this in a single line here I am using <<<
Can anyone suggest how can I execute this script from the PHP?
shell_exec uses dash shell system by default : to make sure run php -r 'echo shell_exec("echo $0");' and it will output 'sh' , and Dash does not have the <<< redirection operator.
Instead you could force the use of Bash and do something like:
shell_exec('/bin/bash -c \'at 12:39 <<< "mkdir newfolder"\'');
Hope this will help.
I have installed Sage math on my ubuntu server. I can run sage commands by ssh terminal such as
$sage:
$sage: f = x^2
$sage: f.diff(x)
I would like to do on a php script
exec('sage');
exec('sage: f = 5x^3');
$fprime = exec('sage: latex(f.diff(x))');
echo $fprime;
I would expect "15x^2" as output but that's not happening .. however on ssh terminal all is good..
all help would be greatly appreciated..
I'm not completely sure of what you want to achieve. PHP's exec function executes a shell command, and that's all. Read http://fr2.php.net/manual/en/function.exec.php.
sage: f = 5*x^3 is not a shell command. You can run sage scripts directly via the command line using the -c switch :
./sage -c 'print latex((5*x^3).diff())'
will print 15 \, x^{2} to the terminal. In PHP you can grab this output by passing a second argument to exec:
exec("./sage -c 'print latex((5*x^3).diff())'", $output);
echo $output[0];
I have a series of shell commands I want to put in a program and execute the program from the command line. I decided to use PHP for this so currently I am trying to get the most basic shell commands to run.
Save as build.php
<?php
shell_exec('cd ..');
echo "php executed\n";
?>
From the command-line
php build.php
Output
php executed
Php executes correctly but I'm still in the same directory. How do I get shell_exec( ... ) to successfully call a shell command?
You need to change the cwd (current working directory) in PHP... any cd command you execute via exec() and its sister functions will affect ONLY the shell that the exec() call invokes, and anything you do in the shell.
<?php
$olddir = getcwd();
chdir('/path/to/new/dir'); //change to new dir
exec('somecommand');
will execute somecommand in /path/to/new/dir. If you do
<?php
exec('cd /path/to/new/dir');
exec('somecommand');
somecommand will be executed in whatever directory you started the PHP script from - the cd you exec'd just one line ago will have ceased to exist and is essentially a null operation.
Note that if you did something like:
<?php
exec('cd /path/to/new/dir ; somecommand');
then your command WOULD be executed in that directory... but again, once that shell exits, the directory change ceases to exist.
I am trying to execute an R script from my PHP page using the exec function. I have set the environment variables in Windows and Rscript works fine on the command prompt. However on the PHP page it says, " 'Rscript' is not recognized as an internal or external command, operable program or batch file."
Any help would be greatly appreciated.
I would define a launcher.bat where I deal will all R-paths problem:
PATH PATH_TO_R/R-version/bin;%path%
cd PATH_TO_R_SCRIPT
Rscript myscript.R arg1 arg2
Then in the php side you can use exec:
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START PATH_TO_LAUNCHER\LAUNCHER.bat');
?>
Im new to php shell commands so please bear with me. I am trying to run the shell_exec() command on my server. I am trying the below php code:
$output = shell_exec('tesseract picture.tif text_file -l eng');
echo "done";
I have the picture.tif in the same directory as the php file. In my shell I can run this without a problem.
It takes a while to run the code, then it doesnt make the text_file like it does when I run it in command prompt.
Per your comment:
Should I write a loop in shell
instead?
You can write a very simple shell script to run the command in a loop. Create the script file first:
touch myscript.sh
Make the script executable:
chmod 700 myscript.sh
Then open it with a text editor such as vim and add this:
for (( i = 0 ; i <= 5; i++ ))
do
tesseract picture.tif text_file -l eng
done
Thats the very basics of it (not sure what else you need), but that syntax should help get you started. To run the script, do this if you're in the same directory as the script:
./myscript.sh
Or specify the full path to run it from anywhere:
/path/to/mydir/myscript.sh
Could this be a permissions issue? My guess is that PHP isn't running with the same permissions that you do when you execute the command directly from the command prompt. What OS are you running on?