gpt4 book ai didi

javascript - 如何通过网页按钮onclick执行linux脚本?

转载 作者:行者123 更新时间:2023-12-01 03:40:18 25 4
gpt4 key购买 nike

我正在解决与通过网络按钮执行脚本相关的问题?当用户单击按钮时,必须运行特定的脚本。我的脚本文件是 auto.sh ,它位于 home/username/auto.sh 脚本文件包括:

#!/bin/bash
cd $home
source /opt/ros/indigo/setup.bash
roslaunch rosbridge_server rosbridge_websocket.launch
exit 0

如何在 html onclick 按钮上运行此脚本?您能一步一步解释解决方案吗?

注意:在我的计算机中已经安装了 Apache Web 服务器和 php。

最佳答案

FMashiro给你正确的答案。根据这个答案,这里有一个例子。

首先您必须确保拥有正确的权限。确保您的用户/组与您的网络文件相同。如果没有更改,请通过:

chown 

你的 html 看起来像这样:

<div class="example">
<button type="button" name="button" onclick="myscriptcaller()">Click</button>
</div>
<script type="text/javascript">
function myscriptcaller() {
$.$.ajax({
url: '/path/to/yourphpfile.php',
type: 'GET',
dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {param1: 'value1'}
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});

}
</script>

请不要将您的文件放在 home/username 中,您的脚本可能位于可从 www 访问但无法从公共(public)区域访问的文件夹中。Executing a Bash script from a PHP script 中讨论和描述

之后,您应该有一个可访问的文件夹,如帖子中所述。

例如/var/www/yourdesireddestination

你的 php 文件:

    <?php
$message=shell_exec("/var/www/yourdesireddestination/auto.sh");
print_r($message);
?>

关于javascript - 如何通过网页按钮onclick执行linux脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com