使用输入参数从php调用shell时出现问题


problems calling shell from php with input parameters

我想从php中调用一个shell脚本并传递输入参数。我对php并不熟悉,但我尝试过使用shell_exec函数,但仍然无法。

<?php
$a = "www.google.com";
echo shell_exec('sh test9.sh' , "$a");
?>

外壳文件是

#!/bin/sh
echo $a

PHP的shell_exec不接受可变参数;您需要将一个字符串参数与整个shell语句一起传递。

echo shell_exec("sh test9.sh '$a'");

此外,如果$a包含不受信任的内容(如用户输入),绝对不要忘记使用escapeshellarg函数转义该值。否则,您将容易受到shell注入攻击。

必须做的事情

 echo shell_exec('sh test9.sh www.google.com');

在sh脚本中,然后是

 echo $1

阅读linux"sh"命令,也许您必须使用sh"/a/b/c/d.sh"的绝对路径,在路径之前没有sh

shell_exec没有第二个参数