Yavuz Selim
Yavuz Selim

Reputation: 546

run php output as shell script

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.

Upvotes: 2

Views: 1077

Answers (1)

HPierce
HPierce

Reputation: 7409

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

Upvotes: 4

Related Questions