在脚本中运行exec

时间:2013-07-22 05:11:30

标签: php

我有这个:

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  exec("./check_sample.sh");
}
?

<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<button type="button" onclick="?run=true">Click Me!</button>

shell脚本check_sample.sh有一些使用printf / echo打印的o / p 当我点击“Click Me”时,我没有看到那些o / p。还有关于如何制作它的任何指针 获取文本输入并将其作为$ 1 arg传递。脚本也会有所帮助

3 个答案:

答案 0 :(得分:1)

exec只会给你最后一行...你可能想要使用passthru

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  passthru("./check_sample.sh");
}
?

对于传递参数,您可以像这样将命令添加到命令中。 (escapeshellarg将为你处理转义和引用值)

  passthru("./check_sample.sh ".escapeshellarg($_POST["fieldname"]));

如果您需要输出作为字符串,您的选项是使用popen或在输出缓冲块中包围passthru:ie:

 ob_start(); 
 /* passthru call */ 
 $data = ob_get_clean();

答案 1 :(得分:1)

exec()没有输出任何内容。您可以使用passthru()

非常小心将用户输入传递给外部程序。如果你确实使用escapeshellarg()确保逃避它。

有点像这样:

passthru('./check_sample.sh '.escapeshellarg($your_user_input));

答案 2 :(得分:0)

exec()只捕获最后一行,似乎你最好使用变量来捕获它。见手册。其他选择是system()shell_exec()passthru(),您可以通过PHP手册找到合适的选项。

相关问题