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!

6
  • Exactly which variable are you trying to put into your HTML file? Commented Apr 22, 2014 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. Commented Apr 22, 2014 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. Commented Apr 22, 2014 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. Commented Apr 22, 2014 at 19:10
  • 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. Commented Apr 22, 2014 at 19:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.