将多个 PHP 变量传递给 shell_exec()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16932113/
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-08-25 11:54:18  来源:igfitidea点击:

Passing multiple PHP variables to shell_exec()?

phpshellshell-exec

提问by user2314387

I am calling test.sh from PHP using shell_exec method.

我正在使用 shell_exec 方法从 PHP 调用 test.sh。

$my_url="http://www.somesite.com/";
$my_refer="http://www.somesite.com/";
$page = shell_exec('/tmp/my_script.php $my_url $my_refer');

However, the command line script says it only received 1 argument: the /tmp/my_script.php

但是,命令行脚本说它只收到 1 个参数:/tmp/my_script.php

When i change the call to:

当我将呼叫更改为:

Code:

代码:

$page = shell_exec('/tmp/my_script.php {$my_url} {$my_refer}');

It says it received 3 arguments but the argv[1] and argv[2] are empty.

它说它收到了 3 个参数,但 argv[1] 和 argv[2] 是空的。

When i change the call to:

当我将呼叫更改为:

Code:

代码:

$page = shell_exec('/tmp/my_script.php "http://www.somesite.com/" "http://www.somesite.com/"');

The script finally receives all 3 arguments as intended.

该脚本最终按预期接收了所有 3 个参数。

Do you always have to send just quoted text with the script and are not allowed to send a variable like $var? Or is there some special way you have to send a $var?

您是否总是必须使用脚本发送仅引用的文本,并且不允许发送像 $var 这样的变量?或者有什么特殊的方式必须发送 $var 吗?

采纳答案by Code L?ver

There is need to send the arguments with quota so you should use it like:

需要发送带有配额的参数,因此您应该像这样使用它:

$page = shell_exec("/tmp/my_script.php '".$my_url."' '".$my_refer."'");

回答by Dave Chen

Change

改变

$page = shell_exec('/tmp/my_script.php $my_url $my_refer');

$page = shell_exec('/tmp/my_script.php $my_url $my_refer');

to

$page = shell_exec("/tmp/my_script.php $my_url $my_refer");

$page = shell_exec("/tmp/my_script.php $my_url $my_refer");

OR

或者

$page = shell_exec('/tmp/my_script.php "'.$my_url.'" "'.$my_refer.'"');

$page = shell_exec('/tmp/my_script.php "'.$my_url.'" "'.$my_refer.'"');

Also make sure to use escapeshellargon both your values.

还要确保对escapeshellarg您的两个值都使用。

Example:

例子:

$my_url=escapeshellarg($my_url);
$my_refer=escapeshellarg($my_refer);

回答by Orangepill

Variables won't interpolate inside of a single quoted string. Also you should make sure the your arguments are properly escaped.

变量不会插入到单引号字符串中。此外,您应该确保正确转义您的参数。

 $page = shell_exec('/tmp/myscript.php '.escapeshellarg($my_url).' '.escapeshellarg($my_refer));

回答by DaoWen

You might find sprintfhelpful here:

您可能会在sprintf此处找到帮助:

$my_url="http://www.somesite.com/";
$my_refer="http://www.somesite.com/";
$page = shell_exec(sprintf('/tmp/my_script.php "%s" "%s"', $my_url, $my_refer));

You should definitely use escapeshellargas recommended in the other answers if you're not the one supplying the input.

escapeshellarg如果您不是提供输入的人,则绝对应该按照其他答案中的建议使用。

回答by bluenapalm

I had difficulty with this so thought I'd share my code snippet.

我对此有困难,所以我想分享我的代码片段。

Before

$output = shell_exec("/var/www/sites/blah/html/blahscript.sh 2>&1 $host $command");

After

$output = shell_exec("/var/www/sites/blah/html/blahscript.sh 2>&1 $host {$command}");

Adding the {}brackets is what fixed it for me.

添加{}括号是为我修复它的原因。

Also, to confirm escapeshellargis also needed.

此外,escapeshellarg还需要确认。

$host=escapeshellarg($host);
$command=escapeshellarg($command);

Except script also needed:

除了还需要脚本:

set host [lindex $argv 0]
set command [lindex $argv 1]

回答by David Jashi

Change

改变

$page = shell_exec('/tmp/my_script.php $my_url $my_refer');

to

$page = shell_exec('/tmp/my_script.php "'.$my_url.'" "'.$my_refer.'"');

Then you code will tolerate spaces in filename.

然后你的代码将容忍文件名中的空格。