0

I and trying to execute a node.js CLI program which takes an input from user to decide the next step.

I am able to start the program by node index.js after this I want to send 1 as input. Is there a way I can give input to my node.js program.

My dumb brain came up with following script

--Script.sh--
node index.js
1
2

which obviously didn't work.

Utpal Dutt
  • 383
  • 3
  • 18

2 Answers2

0

Maybe this would work?

Create an answers.txt file with each input on its own line:

first
second

then run the command like this: node index.js < answers.txt

The < will redirect input to the specified file instead of stdin.

Flet
  • 439
  • 2
  • 2
  • Hey @flet I tried it seems to work a little bit Node.js got input ```first``` but did not waited for ```second``` got terminated after first input. Any idea ? – Utpal Dutt Jan 27 '21 at 05:47
0

What about:

#!/bin/dash
echo "Type something or 'q' to quit and press Enter!"
while read user_input; do
    if [ "$user_input" = 'q' ]; then
        break
    else
        echo "You've typed: ""$user_input"
        # do something
        # node index.js "$user_input"
    fi
done
Darkman
  • 2,941
  • 2
  • 9
  • 14