2
我想點擊一個按鈕的HTML代碼中後執行shell命令,所以:sed命令通過了shell_exec沒有被執行
在我的html代碼:
<form action=execute.php method="post">
<input name="submit" type="submit" value="Reset">
</form>
execute.php:
<?php
$output = shell_exec("sed -i '2,3d' filename.txt");
//$output = shell_exec("cat filename.txt");
echo "<pre>$output</pre>";
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>Back</a>";
?>
其目的是刪除文本文件的所有行,但第一個。
我認爲php代碼正在正確執行,因爲如果我通過一個簡單的貓修改shell命令,它按預期工作,但我無法弄清楚爲什麼當執行的命令是什麼時候什麼也不做,sed
表達。
我已經驗證了這樣的sed
命令在直接在CLI中執行時起作用,並且自從我將「filename.txt」修改爲777以來,我也放棄了任何權限問題。實際上,如果更改php代碼以將一些文本附加到文件也可以。例如:
$output = shell_exec("echo hi >> filename.txt");
尋找信息後,我意識到,我想要的東西可以直接用PHP來實現:
file_put_contents("filename.txt", "");
file_put_contents("filename.txt", "First_line_content");
但現在我只是好奇,爲什麼我的sed
命令未工作。
我運行LAMP在Ubuntu桌面與以下sed
版本:
$ sed --version
sed (GNU sed) 4.2.2