首页 > 解决方案 > Use a shell script output as variable in html

问题描述

I'm learning HTML and I have a question on working with shell scripts.

For example, I have a shell script, which looks like this:

#!/bin/bash

//Get input of text field in HTML
if [$INPUT = "testString"]
then
    echo 'true'
fi

And I have an HTML file, which looks like this:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf8">
        <title>Test</title>
    </head>
    <body>
        <form>
              <input type="text">
              <input type="button" onclick="">
//onclick: execute shell script with value of text field and if shell script returns true, load another page. 
        </form>
    </body>
</html>

Now, I want to know how I can execute the .sh script and how I can handle the output.

Can anybody help me?

标签: htmlbashshellsh

解决方案


我不确定这是否可以在 stackoverflow 问题的范围内得到回答,但我会试一试。最终我认为你有两个选择:

  • 学习 Javascript。
  • 学习 CGI(或更现代的 Web 解决方案,如果 bash 不是必须的)

您需要了解的第一件事是客户端与服务器端编程。如果您正在执行点击,它必须是客户端,这通常意味着它必须是 Javascript;现在有办法在你的浏览器中使用其他语言,但现在,假设它必须是 Javascript。

如果您想将 shell 脚本作为 Web 系统的一部分运行,您将不得不进行一些服务器端编程。对于您正在谈论的简单示例,我建议您学习 CGI;通用网关接口。对于真实的东西,我觉得它在这一点上已经过时了,但对于你的简单情况,它很简单,将成为学习更有用的东西(如 Python WSGI、PHP 或 FastCGI 等)的良好基础。

您并没有真正点击服务器端程序(自 AJAX 以来技术上不正确,但现在假设如此);您将表单操作设置为调用服务器端程序。

<form action="serverside.cgi" method="POST">
  <input type="text">
  <input type="submit">
</form>

现在您需要启用 CGI 的 Web 服务器和 CGI​​ 的基本教程(如果您想使用 bash,但请考虑 Python(Flask) 和 PHP,或您可能熟悉的任何其他语言的 Web 功能/工具)好)这远远超出了可以合理地呈现为堆栈溢出答案的范围。


推荐阅读