【问题标题】:PHP execute shell script and provide inputs for promptsPHP 执行 shell 脚本并为提示提供输入
【发布时间】:2017-06-20 16:12:30
【问题描述】:

我在树莓派上有一个 apache 服务器。我有一个基本程序,用户在其中将字符串输入到 html 表单框中。单击提交将条目发送到 php 文件,然后该文件调用 shell 脚本,并将条目作为提示传递。如果你看下面的 php,它有一个带有用户输入的变量,它调用 shell 文件 main.sh。如何提供该变量作为输入的 shell 程序。我查看了 proc_open,但无法让它工作。谢谢 索引.html:

    <!DOCTYPE html>
<html>
<head>

</head>
<body>
    <form action="script.php" method="post" enctype="multipart/form-data">
    Address: <input type="text" name="Address" value=""><br>
    <input type="submit" value="Upload" name="submit">
    </form>
</body>
</html>

script.php:

<?php
if(isset($_POST["submit"])) {
    if($_POST['Address'] != "")
    {
        $entry = $_POST['Address'];
        $output  = shell_exec ("./main.sh");
        echo $output;
    }
}
?>

最后是 shell 文件:main.sh:

echo -n "Do you like pie (y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
    echo Yes
else
    echo No
fi

【问题讨论】:

    标签: php html shell


    【解决方案1】:

    您可以将变量传递给 shell 脚本,如下所示

    $output = shell_exec("./main.sh $entry");

    在 shell script 中,添加 x = $1 。 $x 将是可变的,具有您的用户输入值。

    查找此帖子以获取更多详细信息https://*.com/a/19460686/3086531

    【讨论】:

    • 好的,我稍后试试,谢谢
    • 我查看了您附加的链接,它有效,我只是在我之前测试此方法时忘记将我的 php var 设置为转义 shell arg。谢谢,
    最近更新 更多