PHP + Shell Returning HTML


PHP + Shell Returning HTML

我目前遇到一个问题,我正在尝试执行一个shell脚本,该脚本解释一个文件并echos back html,shell脚本在本地执行得很好,但当我尝试通过php执行时,它不会返回任何东西,而且似乎php根本没有执行它,我有一个文件,它是在执行脚本时创建的,在php执行时不会显示出来。我有其他用php执行的脚本,它们运行得很好,我有适当的www数据权限来执行这个文件。请帮帮我,这是我试图执行的脚本和执行它的php文件。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tbody>
    <tr>
      <th style="font-size: 24px" scope="col"><p align="left" class="title_Font">Magnum Dongs <span style="font-size: 18px">&nbsp;|&nbsp; Mighty Meat Sticks 4 Kidz</span></p></th>
    </tr>
  </tbody>
</table>
<ul align="center" width="1000" height="50">
  <li><a href="#home">Home</a></li>
  <li><a href="#news">Stories</a></li>
  <li><a href="#contact">Memes</a></li>
  <li><a href="#about">Shop</a></li>
  <li><a href="#email">Email</a></li>
</ul>
<table width="1000" height="386" border="0" align="center" cellpadding="0" cellspacing="0" class="body_Main">
  <tbody>
    <?php 
    echo exec('./var/www/html/newdongs/fetch_html.sh');
    ?>
  </tbody>
</table>
</body>
</html>

#!/usr/bin/env bash
python3 /bin/dongs/db_Read.py >> lines
#1
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==11' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==41' lines`"</p>"
echo "<img src='"`awk 'NR==31' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==21' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#2
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==12' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==42' lines`"</p>"
echo "<img src='"`awk 'NR==32' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==22' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#3
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==13' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==43' lines`"</p>"
echo "<img src='"`awk 'NR==33' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==23' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#4
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==14' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==44' lines`"</p>"
echo "<img src='"`awk 'NR==34' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==24' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#5
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==15' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==45' lines`"</p>"
echo "<img src='"`awk 'NR==35' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==25' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#6
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==16' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==46' lines`"</p>"
echo "<img src='"`awk 'NR==36' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==26' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#7
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==17' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==47' lines`"</p>"
echo "<img src='"`awk 'NR==37' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==27' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#8
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==18' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==48' lines`"</p>"
echo "<img src='"`awk 'NR==38' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==28' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#9
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==19' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==49' lines`"</p>"
echo "<img src='"`awk 'NR==39' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==29' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"
#10
echo "<tr>"
echo "<th scope='col'>"
echo "table width='1000' border='0' class='post' align='center' style='margin-top: 10px;'>"
echo "<tbody>"
echo "<tr>"
echo "<th scope='col' width='50'>"
echo "<p style='font-size: 0px; margin:0px; ' align='center'> Posted By: "`awk 'NR==20' lines`"</p>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Total Score: "`awk 'NR==50' lines`"</p>"
echo "<img src='"`awk 'NR==40' lines`"'  width='150' height='150' alt=''/>"
echo "<p style='font-size: 0px; margin: 0px; ' align='center'> Upvote | Downvote</p>"
echo "</th>"
echo "<th scope='col' align='left' valign='top'>"
echo "<p class='post' style='font-size: 14px'>"`awk 'NR==30' lines`"</p>"
echo "</tr>"
echo "</tbody>"
echo "</table></th>"

感谢您的提前帮助!

我认为问题在于PHP在错误的位置查找脚本。

您可以使用getcwd()获取当前工作目录。检查您是否使用了当前工作目录中脚本的正确相对路径。

否则,您可以简单地将脚本的绝对路径传递给exec()命令。

下一个问题是shell脚本打印了相当多的行,并以标记</table></th>结束。exec()函数只返回shell脚本输出的最后一行。因此,PHP输出将只显示</table></th>。这不能在HTML中呈现,因为它们只是结束标记。因此,你在浏览器中看不到任何变化,但如果你检查HTML,你可以注意到它。

相反,您可以使用shell_exec()来返回shell脚本的整个输出。

以下是更正后的版本:

<?php 
// 1.Remove the leading dot. The leading dot makes the path relative.
// 2.Use shell_exec instead to get all lines from your script output.
echo shell_exec('/var/www/html/newdongs/fetch_html.sh'); 
?>