简体   繁体   English

从 shell 脚本自动化 Node.js CLI 程序

[英]Automate Node.js CLI program from shell script

I and trying to execute a node.js CLI program which takes an input from user to decide the next step.我并尝试执行 node.js CLI 程序,该程序接受用户的输入来决定下一步。

I am able to start the program by node index.js after this I want to send 1 as input.在此之后我想发送1作为输入,我可以通过node index.js启动程序。 Is there a way I can give input to my node.js program.有没有办法可以输入我的 node.js 程序。

My dumb brain came up with following script我的愚蠢大脑想出了以下脚本

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

which obviously didn't work.这显然没有用。

Maybe this would work?也许这会起作用?

Create an answers.txt file with each input on its own line:创建一个 answers.txt 文件,每个输入都在自己的行中:

first
second

then run the command like this: node index.js < answers.txt然后像这样运行命令: node index.js < answers.txt

The < will redirect input to the specified file instead of stdin. <会将输入重定向到指定的文件而不是标准输入。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM