2

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.

2
  • If you have root access to the server, surely you can just enable shell_exec() in php.ini, right? Commented Nov 18, 2015 at 16:41
  • This is shared server I cannot enable shell_exec for security reasons. Commented Nov 18, 2015 at 16:53

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.