0

I only see topics passing js values to a bash script. I want values from a bash script to be used in a javascript.

What I want:

Based on a signal-number, a different icon should be displayed on my webpage. The signal-number needs to be checked every 5 seconds so the icon can be updated accordingly. (Like the signal quality bar on a mobile phone).

What I have:

A bash file called signal.sh which contains the following line to execute:

/usr/bin/pretty-wifi-info.lua | grep 'Signal' | tr '%' ' ' | awk '{printf "{\"Signal\":"$2"}"}'

The output from that script is (for example):

{"Signal":55}

In other words a JSON-object. The bash script is located on my server at /www/signal.sh

What I want to know:

1) How can I use javascript to run this bash script, get that json-object out and use the number to do a calculation in js on my website (pretty much like using setInterval to re-run a php-file that ouputs a json-obj get the info stored in a variable and than update a div with that new variable).

2) How to attach a different icon to a specific value.

About the first one. I really don't have a clue!

The latter should -I think- be a if, else if, else statement that depending on the outcome returns a specific img_src which is used to display the icon in a < div> or on a < span> which updates every 5 seconds.

Hope someone can help!

UPDATE

I ticket MichaelWclark's answer as the correct one. I used his info to display the correct icon. I decided not to go down the road of node.js but use a php file instead that reads the bash script.

Arjan_IO
  • 175
  • 1
  • 9
  • 3
    that's not an array. `{}` in JS is an object. where is this js code, and how does it execute bash? – Marc B Feb 03 '16 at 20:33
  • Possible duplicate of: [Node.js Shell Script And Arguments](http://stackoverflow.com/questions/7464036/node-js-shell-script-and-arguments) – neilsimp1 Feb 03 '16 at 20:53
  • Sorry, I meant object. Shame on me. I refrased the question in my OP. Hope it is more clear what I try to achieve. I'll check the link provided by neilsimp1. – Arjan_IO Feb 04 '16 at 05:33
  • From what I read this is more complex than it looks at first. If I am correct, node.js is server-sided. At least, I made a test file and is came back with `ReferenceError: require is not defined` when opening it in the browser. So I guess I need to make a .js file to get the stuff out of the .sh file. Then, somehow, I need to get the console.log into the browser-side to process it and get my relevant icon. I wonder if there would be no simpler way to get the info out the .sh file. Or have I missed something? – Arjan_IO Feb 04 '16 at 11:28

1 Answers1

2

Just an idea.. I really have no clue what your trying to achieve, but if I'm on the same page here this might work.

Setup a cron job to run every x minutes or seconds with following command:

/usr/bin/pretty-wifi-info.lua | grep 'Signal' | tr '%' ' ' | awk '{printf "{\"Signal\":"$2"}"}' > /home/username/webFolder/signal.json

In your JavaScript do something like:

<script type="text/javascript"  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script type="text/javacript">
var src = "http://www.myImage.com/somepicture.jpg";
$.getJSON("signal.json", function(json) {

    if(json.signal == 53){ 
     src = "http://www.myImage.com/signal53.jpg";
    } else if (json.signal == 54) {
     src = "localImageSignal54.jpg";
    }
    $("#img").innerHTML= "<img src='"+src+"' />";
});
</script>
<div id="img"></div>

This isn't tested, but hopefully will get you started. Essentially it's reading a json file locally, then based upon the signal attribute of the json object it's setting an image source. When it's done with that it's adding the appropriate image to the DOM.

MichaelWClark
  • 382
  • 1
  • 12