0

I'm trying to take simple input from a linux shell when a javascript program is run. I've tried using readline() and prompt() but both of those throw Reference Error: readline() is not defined or prompt() is not defined.

//Decode Bluetooth Packets

var advlib = require('advlib');
console.log("What data to process - If you respond N than what is written inline will be decoded");
var input = require();
if (input != "N") {

    var rawHexPacket = input
    var processedpacket = advlib.ble.process(rawHexPacket);
    console.log(JSON.stringify(processedpacket,null, " "));
}
else {  
    //Put in raw data here!
    var rawHexPacket = 'dfasdfasdfasd4654df3asd3fa3s5d4f65a4sdf64asdf';
    var processedpacket = advlib.ble.process(rawHexPacket);
    console.log(JSON.stringify(processedpacket,null, " "));
}

So what is a simple way to get javascript input through a linux shell?

4

1 Answer 1

0

I used the link posted and turned it into this (which works):

var advlib = require('advlib');

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
try {
    rl.question("What data to process - If you respond N then what is written inline will be decoded. ", function(answer) {
        console.log("You said: ",answer);
        if (answer != "N") {

            var rawHexPacket = answer
            var processedpacket = advlib.ble.process(rawHexPacket);
            console.log(JSON.stringify(processedpacket,null, " "));

        }
        else {  
            //Put in raw data here!
            var rawHexPacket = '';
            var processedpacket = advlib.ble.process(rawHexPacket);
            console.log(JSON.stringify(processedpacket,null, " "));

        }
    });
}
catch(err) {
    console.log("Somthing went wrong - was your input valid?");
};
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.