HTML 中的連結無法執行 .sh 文件

HTML 中的連結無法執行 .sh 文件

我有一個index.html鏈接,必須從 中刪除所有 *.zip /mnt/sda1/down

當我單擊連結時,它開始下載而不是執行 sh 腳本。

這是index.html和 sh 腳本:

#!/bin/sh
cd /mnt/sda1/down
rm *.zip 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label><a href="delete.sh">DELETE ALL</a>  </label>
</form>
</body>
</html>

答案1

您的鏈接是指向您的瀏覽器無法識別的文件的鏈接,因此它假定它是下載。

將該連結放入 a 中<form>不會改變任何內容。該元素的用途<form>是在呼叫「action」屬性中指定的 URL 之前收集參數(通常使用<input type="submit">表單中的元素)。

甚至不清楚是否涉及伺服器。如果是的話,它是什麼類型?

答案2

HTML 不具備這種能力。您需要使用 PHP,因為它是一種後端語言,可以與您的作業系統互動。

您可以做的是創建一個指向 php 文件的鏈接,當單擊該鏈接時,它將導航到 PHP 文件,執行腳本,然後您可以告訴它將您重定向回主頁或其他內容:

索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<a href="delete.php">DELETE ALL</a>
</body>
</html>

刪除.php

<?php
echo shell_exec('sh /path/to/delete.sh');
header('Location: /'); #this will take you back to the home page
?>

原帖:https://stackoverflow.com/questions/7397672/how-to-run-a-sh-file-from-php

相關內容