2

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!

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • Exactly which variable are you trying to put into your HTML file? – merlin2011 Apr 22 '14 at 17:45
  • The signal strength of the modem is obtained by at+csq command, I am REALLY green to coding but I would guess that "MODEMCMD" is being used as variable as it equals the AT command in the beginning of this script. – Coder that Could Apr 22 '14 at 18:54
  • I have a local webpage that runs, and in a corner I have a table element. In part of that table I would like the raw signal value (which is obtained by the at+csq) to be displayed and auto updated every 10seconds. I am building a small signal tester so this device has a small screen that displays this webpage, allowing someone to walk around and easily see signal strength in a certain area quickly. – Coder that Could Apr 22 '14 at 19:06
  • If you want dynamic updates to a web page, you will need to learn AJAX. Piping a shell script into an HTML page may not cut it. – merlin2011 Apr 22 '14 at 19:10
  • Once piped to html, my thinking was to use "" (this would reload entire page every 10 sec) This is more internal use so it can be a bit rough and not so dynamic – Coder that Could Apr 22 '14 at 19:14
  • 1
    I just modified the tags on your question, so you will hopefully be able to get help from someone who knows `bash` well enough to be able to tell you how feasible this is. However, you should at the very least show a part of the `html` you are trying to substitute into. – merlin2011 Apr 22 '14 at 19:18

1 Answers1

2

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.

Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45