Question

Rather new to coding, looking for a little help with having a variable output to a local html file. Both script and html are on the same machine. Script pulls signal level from a modem and I would like to have that displayed to a local html loading screen.

Signal Script:

#!/bin/bash

MODEM=/dev/ttyUSB3
MODEMCMD=AT+CSQ

{
  echo -ne "$MODEMCMD"'\r\n' > "$MODEM"

  if [ $? -eq 1 ]
  then
    echo "error";
    exit;
  fi

  {
    while read -t 1
    do
      if [[ $REPLY == +CSQ* ]]
      then
        arr1=$(echo "$REPLY" | cut -d' ' -f2)
        arr2=$(echo "$arr1" | cut -d',' -f1)
        echo "$arr2"
        exit;
      fi
    done
    echo "error";
  } <"$MODEM"
}  2>/dev/null

Would like the output of this to display in a table on my html. Thanks!

Was it helpful?

Solution

When you host your own web server, the CGI protocol allows you to do server-side programming in any language you like; including Bash.

Here's a simple example that serves a web page that displays the current date and time, and refreshes every 10 seconds. Put the Bash script below in a file named test.cgi in the cgi-bin folder (on Linux/Apache: /usr/lib/cgi-bin) and make it executable (chmod +x test.cgi). The page's URL is: http://MyWebServer/cgi-bin/test.cgi

#!/bin/bash
cat << EOT
content-type: text/html

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="refresh" content="10" />
</head>
<body>
$(date)
</body>
</html>
EOT

Please note: the empty line below content-type is relevant in the CGI protocol.

Replace date by your own Bash script; make sure it outputs something that resembles valid HTML. This means you need to take some effort to add HTML tags for your markup, and HTML-encode some characters (< > &) in your content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top