gpt4 book ai didi

javascript - 如何通过 html "JS file"调用 "button-click"并且 JS 文件正在使用 shell.js 运行 bash 脚本

转载 作者:行者123 更新时间:2023-11-30 11:08:34 24 4
gpt4 key购买 nike

我想通过单击 html 按钮连接到“server.js”文件。

“server.js”文件正在使用 shell.js 运行“bash.sh”文件

任何人都可以就如何进行提供一些想法或指导吗?

按钮.html

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script src="server.js"></script>

</body>

客户端.js

function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}

服务器.js

const shell = require('shelljs');
shell.exec('./bash.sh')

bash.sh

#!/bin/bash

printf "command 1 is running...\n\n"

mlpack_linear_regression --training_file aircon.csv -v -M lr.xml

printf "\ncommand 2 is running...\n\n"

mlpack_linear_regression --training_file aircon.csv --test_file
predict.csv --output_predictions_file prediction.csv -v

printf "\nPredicted Output: \n\n"

cat prediction.csv # after this command the result should shown on
#the browser screen


echo "hello" >> file # To test if connection is happening or not
#by writing a string in a file

最佳答案

查看 shell.js 的文档.它说:

Portable Unix shell commands for Node.js

您正在尝试在网络浏览器中使用它。它不是为在网络浏览器中运行而设计的,因此无法在该浏览器中运行。

您需要使用 Node.js 运行它。


可以使用 Node.js 编写一个 HTTP 服务器,然后使用 Ajax(或只是一个常规的链接点击或表单提交)向该服务器发出 HTTP 请求,使其执行任何操作你想对 shell 这样做。

关于javascript - 如何通过 html "JS file"调用 "button-click"并且 JS 文件正在使用 shell.js 运行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692349/

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