从PHP启动的脚本无法正常工作,但它可以从命令行执行

时间:2016-02-04 18:41:57

标签: php shell sh

我从php脚本创建并运行shell脚本。该脚本有ifthen上的命令不会在应用时运行,而是else中的命令在不应该运行时运行。当我从命令行运行相同的脚本时,它按预期工作。

php文件:

<?php
 .....
$msg = "#! /bin/sh\n\n";
file_put_contents($scriptfile, $msg, LOCK_EX);
$cmd = "/usr/bin/pdftohtml -noframes -q -p -s -i $renfilenc\n";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
$noExtfile = preg_replace('"\.(pdf|PDF)$"', '', $renfilenc);
$cmd = "if [[ \$(grep -c -o \"</p>\" $noExtfile.html) -le 5 ]];
then
    /usr/bin/gs -dNOPAUSE -q -r500 -sDEVICE=tiffg4 -dBATCH -sOutputFile=$noExtfile.tiff $renfilenc
    /usr/bin/tesseract $noExtfile.tiff $noExtfile -c tessedit_create_hocr=1 -c hocr_font_info=1
else
    /usr/bin/unoconv -f docx $noExtfile.html
    /usr/bin/curl -i -F \"Profile=jsi\" -F \"Output=url\" -F \"Language=en\" -F \"infile=@$noExtfile.docx\" -F \"submit=Submit\" http://nl.ijs.si/tei/cgi/convert.pl > TEIconvert.log
    grep -E -o \"http://.*\" TEIconvert.log > wgetline.log
    sed -r \"s/^(.*).$/wget \\1\/tei.xml -O $noExtfile.xml/g\" wgetline.log > wgetTEIfile.sh
    /bin/sh  wgetTEIfile.sh
fi";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
$cmd = "/bin/sh  $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);
.....
?>

创建的脚本文件:

#! /bin/sh

/usr/bin/pdftohtml -noframes -q -p -s -i USNews_Bostonian_image.pdf
if [[ $(grep -c -o "</p>" USNews_Bostonian_image.html) -le 5 ]];
then
   /usr/bin/gs -dNOPAUSE -q -r500 -sDEVICE=tiffg4 -dBATCH -sOutputFile=USNews_Bostonian_image.tiff USNews_Bostonian_image.pdf
   /usr/bin/tesseract USNews_Bostonian_image.tiff USNews_Bostonian_image -c tessedit_create_hocr=1 -c hocr_font_info=1
else
   /usr/bin/unoconv -f docx USNews_Bostonian_image.html
   /usr/bin/curl -i -F "Profile=jsi" -F "Output=url" -F "Language=en" -F "infile=@USNews_Bostonian_image.docx" -F "submit=Submit" http://nl.ijs.si/tei/cgi/convert.pl > TEIconvert.log
   grep -E -o "http://.*" TEIconvert.log > wgetline.log
   sed -r "s/^(.*).$/wget \1\/tei.xml -O USNews_Bostonian_image.xml/g" wgetline.log > wgetTEIfile.sh
   /bin/sh  wgetTEIfile.sh
fi

在这种情况下,then上的命令应该运行,而不是else上的命令。 如果出现问题或遗失,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我在这里找到答案:https://superuser.com/questions/374406/why-do-i-get-not-found-when-running-a-script

所以在我的php中我改变了:

$msg = "#! /bin/sh\n\n";

$cmd = "/bin/sh  $scriptfile > /dev/null 2>&1 &";

$msg = "#!/bin/bash \n\n";

$cmd = "/bin/bash  $scriptfile > /dev/null 2>&1 &";
分别