-2

I want to copy a javascript code snippet into Nodejs REPL using shell script. For example

sum.sh

node // Here REPL open, I want copy below codes to this REPL and run

var num1 = 1
var num2 = 2
var sum = num1 + num2
sum

After run sum.sh, expecting result

3

Can we have a solution?

Renjith
  • 2,025
  • 3
  • 12
  • 20
  • You can't. That's not how it works. What is the actual problem you're trying to solve? – Jonathan Hall Nov 17 '18 at 18:32
  • @Flimzy You mean we can't insert the js code into node repl using shell script? – Renjith Nov 17 '18 at 18:34
  • I don't know what you mean by "insert js code into node repl". – Jonathan Hall Nov 17 '18 at 18:35
  • @Flimzy when we run **sum.sh**, when the 1st line executes, the node REPL terminal will open. I want line 2 to 5 codes to be inserted to that REPL. Is it possible? – Renjith Nov 17 '18 at 18:38
  • Your question as it stands is not clear. Couple of things you can do: 1. Rephrase it and add more details 2. Rethink about the problem you're trying to solve and see if what you're doing is the best approach. – Samuel Nov 17 '18 at 18:50
  • @Samuel Sorry about that. I've updated the question. Do you get it now what I meant? – Renjith Nov 17 '18 at 18:56
  • Not quite sure I get it but let's see, if you want to run `sum.sh` then it means you're not in Node REPL, you are in a normal terminal, am I getting this correct? – Samuel Nov 17 '18 at 19:01

2 Answers2

0

JavaScript does not run in the terminal. To run JavaScript in a Node.js file, you would install Node.js, create the script, and then run it from the terminal.

First, create the Node.js file which we'll call sum.js:

let num1 = 1;
let num2 = 2;
let sum = num1 + num2;
console.log(sum);

To run this file from the terminal we would type node sum.js.

To run this from a script, simply create a script, sum.sh with executable permissions to have the following: node sum.js

It's the same thing as running it from the terminal, but now it's in a script file.

JSBach
  • 187
  • 2
  • 11
  • Thanks for your reply. But this is not what I meant. I need to be run node REPL and insert the JS code to it and then execute. – Renjith Nov 17 '18 at 18:40
  • 1
    It is unclear what you are asking. Can you restate the question, please? – JSBach Nov 17 '18 at 18:42
  • Sorry about that. I've updated the question. Do you get it? – Renjith Nov 17 '18 at 18:54
  • I apologize, but I still do not understand, partially because I do not understand why you want to intermix the script with the program. If you want to have the Node code sum the numbers, then just do it in the loop. – JSBach Nov 17 '18 at 18:57
0

You can't directly copy paste command into NodeJS REPL. I think you should try this one.

run command node and then running on REPL cli as follows .load sum.sh.

This would be run line by line as expected.

Reference. How do I load my script into the node.js REPL?

YGautomo
  • 619
  • 8
  • 12