PHP调用带有多个命令的bash脚本问题


php calling bash script with multiple commands issue

下面是任务

的演示文件

index.php目录

shell_exec("./delete.sh param1 params2");

脚本可执行

demo.sh

#! /bin/bash
#! /usr/local/bin/php
destination_path=$1
source_path=$2
xpdf/pdftotext source_path destination_path && php textconv.php destination_path final_output

当从终端执行demo.sh脚本时运行良好,并生成所需的输出,但当通过index.php.

执行时则不运行。

生成了destination_path,但没有生成final_output。有人能帮忙吗?&&是问题的根源吗?

@GeraldSchneider感谢您的及时评论,它对得出结论有很大帮助。

首先,我使用的shebang是错误的,我必须删除

之间的空格

其次,我只能在第一行使用一个解释器所以我从上面删除php并在我的命令中使用它,如下所示

xpdf/pdftotext source_path destination_path && /usr/local/bin/php textconv.php destination_path final_output

最后为了调试的目的添加2>&1是一个好方法

最终脚本

#!/bin/bash
destination_path=$1
source_path=$2
xpdf/pdftotext source_path destination_path && /usr/local/bin/php textconv.php destination_path final_output

执行

$output = shell_exec("./delete.sh param1 params2 2>&1");
echo $output;