在 PHP 中使用 bash shell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4701993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:16:37  来源:igfitidea点击:

Using bash shell from within PHP

phpbashshell

提问by Dan

In my terminal window (using Max OS X) my shell is bash. However when I run a command in PHP via shell_exec or backtick operators I see that PHP is using the Bourne Shell (sh). Here's an example of what I'm seeing:

在我的终端窗口(使用 Max OS X)中,我的 shell 是 bash。但是,当我通过 shell_exec 或反引号运算符在 PHP 中运行命令时,我看到 PHP 正在使用 Bourne Shell (sh)。这是我所看到的一个例子:

From within my terminal window:

从我的终端窗口中:

$ echo 
$ php -r "echo shell_exec('echo 
<?php echo shell_exec('echo 
$ php test.php
sh
'); ?>
');" -bash
- bash

Also if I call php as follows:

另外,如果我按如下方式调用 php:

$ php -r 'echo shell_exec("echo 
php -r '$cmd="echo \$0"; echo shell_exec("/bin/bash -c \"$cmd\"");'
");' sh

However, if I create a script called test.php with the following:

但是,如果我使用以下内容创建一个名为 test.php 的脚本:

shell_exec("bash script.sh");

And then run test php I get the following:

然后运行 ​​test php 我得到以下信息:

##代码##

I'm wanting to use the bash shell when calling shell_exec - why is it choosing the Bourne shell and can I force it to use bash?

我想在调用 shell_exec 时使用 bash shell - 为什么它选择 Bourne shell,我可以强制它使用 bash 吗?

Thanks!

谢谢!

Dan

回答by Paused until further notice.

Reverse the quotes in your second command:

反转第二个命令中的引号:

##代码##

With the quotes as you had them in your question, the variable $0is expanded before the command is sent to php.

使用问题中的引号,$0在命令发送到php.

If you want to force the use of Bash, you could do something like:

如果您想强制使用 Bash,您可以执行以下操作:

##代码##

回答by Shadikka

It probably reads the SHELL environment variable.Disregard that, putenv() didn't work.

它可能读取 SHELL 环境变量。忽略这一点, putenv() 不起作用。

Try to just run the command you want with bash, like

尝试使用 bash 运行您想要的命令,例如

##代码##

回答by Maxim Gueivandov

Assuming that:

假如说:

  1. your PHP script is executed by the web server (say, Apache)
  2. the web server is executed with the rights of a special user account,
  1. 您的 PHP 脚本由 Web 服务器(例如 Apache)执行
  2. 使用特殊用户帐户的权限执行 Web 服务器,

one of the possible (but not optimal) solutions would be to change the default shell of the user account under which your web server is executed.

一种可能的(但不是最佳的)解决方案是更改执行 Web 服务器的用户帐户的默认 shell。