如何通过html“按钮单击”调用“JS文件”,JS文件使用shell.js运行bash脚本
我想通过单击html按钮连接到“server.js”文件。
“server.js”文件使用shell.js运行“bash.sh”文件
谁能提出一些关于如何进行的想法或指示?
button.html
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script src="server.js"></script>
</body>
client.js
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
server.js
const shell = require('shelljs');
shell.exec('./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的文档。它说:
Node.js的可移植Unix shell命令
您正尝试在Web浏览器中使用它。它不是设计用于在Web浏览器中运行,也不会在那里工作。
您需要使用Node.js运行它。
您可以使用Node.js编写HTTP服务器,然后使用Ajax(或只是常规链接点击或表单提交)向该服务器发出HTTP请求,以使其对shell执行任何操作。