I have installed PHP CLI to execute php commands from console.
I have installed PHP CLI using this command -
sudo apt-get install php5-cli
When I run this
$vr=3; echo $vr;
Result :-
=3: command not found
If I run echo "test";
Result :- test
displays..
Can anyone tell why "command not found" displays..
The "echo "test" line is working because echo is a bash command.
You have to write your own php script, the run it by command line like this:
$ php myscript.php
In alternative, you can run php from your command line, then directly write or paste your script.
Then press CTRL+D to run it. Remember the at the beginning and at the end.
As third option, you can write a php script, putting in the first line this code:
#!/usr/bin/php
Obviously the php executable path must match the one in your system.
This way, you can chmod +x the script, then run it directly like this:
$ ./myscript.php
The fourth option is the interactive shell:
$ php -a
Interactive shell
php > echo 5+8;
13
[$ in front of commands means a command run by user]
You are entering PHP code into the Unix shell (e.g. bash). The Unix shell does not understand PHP code, so you have to run php first.
To run your PHP code from the command line:
$ php -r '$vr=3; echo $vr, "\n";'
3
To run your PHP code from the PHP interactive shell (which may or may not be compiled into PHP):
$ php -a
Interactive shell
php > $vr=3; echo $vr, "\n";
3
php >
(Hit Ctrl+D or type exit to get out of the PHP shell.)
To run your PHP code from a file named prog.php (which contains <?php before the code):
$ php prog.php
3
It seems you want something like this: http://www.php.net/manual/en/features.commandline.interactive.php
This gives you an interactive mode, where you can type PHP code and have it executed directly.
Related
Hello i have a PHP script, and its added to cron, it is possible to execute from this script shell command (with exec() or something) without enabling it on php.ini? I don't want to enable exec on my site
It's called PHP CLI, check here
Usually when you install php, there's option to install php_cli too.
So long you can run php on shell prompt, then it can work.
Open bash (or other shell), try this:
php -v
If the version printed, then it's working.
Then you can
php -f phpfile
or put
#!/usr/bin/php
At the beginning of your php file as a line, and chmod +x file.php, and then
./file.php
#or
/path/to/file.php
to run it.
(Note /usr/bin/php is the usual place of php executable, it might change, eg in unix is ually /bin/php. Use whereis php to check its place.)
How can I launch a php script from Linux console?
For example, I have myscript.php file. I should be able to launch it like myscript from console (I can create and use any other scripts)
Also, I should be able to send parameters to it like myscript dosome [-n <count>]. Can I do this?
You could find it with very simple Google search.
So you should install PHP-CLI first. For example:
sudo apt-get install php5-cli
After that you should write some code to your php file:
nano example.php
and write
<?php phpinfo(); ?>
For run this script you should give permission:
sudo chmod 755 example.php
And can run it as:
./example.php
To run php script from shell with argumets you must create bash script, name it myscript and add this code (change php to your php installation dir)
"php" myscript.php $*
And add script folder to Linux PATH
$ export PATH=$PATH:/path/to/folder/
Then you can access the script with myscript arg1 -n 20. You can access arguments via $argv variable in php script
I want run php ../cat1/index.php gr_s2/3/gr-n40 1200 command by php.
The shell_exec return NULL as result but when I try that command on cmd, output was shown correctly.
What happen in php and shell_exec ?!
Note: The command with different parameters (Like: php ../cat1/index.php gr_s2/3/gr-n40 800) works correctly in both (php and cmd).
There is a note in php manual page of shell_execute:
Note: This function can return NULL both when an error occurs or the
program produces no output. It is not possible to detect execution
failures using this function. exec() should be used when access to the
program exit code is required.
Source: http://php.net/manual/en/function.shell-exec.php
So your code running with error. Try with exec.
If you want, insert your code (or blocks) to be checked.
This is fixed by adding user used by web-server to sudoer and run the php command with
sudo php ..........
To start run this command sudo visudo
Add the following line at the end of sudoer file opened
www-data ALL=NOPASSWD: /usr/bin/php
But if you whant to execute all commad from php add this line instead of the above www-data ALL=NOPASSWD: ALL which is not recommended
and the run your commands with sudo php /path/to/you/file.php
For my case I was running ubuntu 14.04
Have fun please
I want to use shell and php together.
First I tried:
shell_exec vs functions is disabled and must be so.
But php does not give me permission to run shell_exec()
So, I gave up and tried to make a .sh, call php,store output of php as sh file and run sh file.
Here is sh code
#!/bin/bash/
php test.php -> running php file/ test.php saves commands in script.sh
sh script.sh -> running the commands
rm script.sh -> removing the commands
But there must be a better way from this file process.
Can I run output of test.php directly in .sh file?
Can I run shell_exec ?
Note: I have root access of the server.
You can pipe (|) the output of PHP to sh to be executed:
$ php -r 'echo "echo \$PATH";' | sh
outputs:
/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/MAMP/Library/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I've read some statements using -r for simplicity, but you can read from a file by passing a script to php:
$ php test.php | sh
I'm trying to create a shortcut script for setting some environment variables and the include path before running php -a. So it looks like this:
#!/usr/bin/env php
<?php
putenv("ENVVAR=value");
passthru("php -a -d include_path=<path>");
This mostly works, but the php shell doesn't correctly output its prompt.
I.e. normal output from php -a:
$ php -a
Interactive shell
php > echo "hi\n";
hi
php >
Output from this script:
$ ./myscript.php
Interactive shell
echo "hi\n";
hi
Additionally, there is no support for walking through the history (or even right or left along what's already been typed) with arrow keys.
Is there a way to get this to work correctly?
I'm using Mac OS X, PHP 5.4.27. I have already tried redirecting stderr to stdout, in case that was somehow the cause (it wasn't.)
Rather than writing it in PHP, why not do it as a shell script.
#!/bin/bash
export ENVVAR=value
php -a -d include-path=whatever
I'm using bash, and it works fine under Ubuntu 12.04. I'm not sure what shell is available on OSX.