Shell脚本输入不适用于npm init [英] Shell script input doesn't work for npm init

查看:40
本文介绍了Shell脚本输入不适用于npm init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习在终端中编写Shell脚本.我正在尝试自动化npm init进程,只是为了进行练习.因此,我从终端读取了所有参数,然后尝试使用它们自动执行 npm init .但是,当 npm init 询问入口点时,该过程总是中止.在其他任何地方只需留下空白行以模拟Enter键,或使用该变量即可正常工作.为什么它在此时退出?

I'm learning to write shell scripts in terminal. I'm trying to automate the npm init process, just for the sake of an exercise. So I read in all the parameters from terminal, then try to automatically execute an npm init with them. However, the process always aborts when npm init asks for the entry point. Anywhere else just leaving a blank line to simulate an enter key press, or using the variable works fine. Why does it quit at that point?

read -p "Project name: " name;
read -p "Version: " version;
read -p "Project description: " description;
read -p "Entry point: " entryPoint;
read -p "Test command: " testCommand;
read -p "Git repository: " gitRepo;
read -p "Keywords: " keywords;
read -p "Author: " author;
read -p "License: " license;
read -p "Is this okay?: " isOkay;

npm init <<!
$name
$version
$description
$entryPoint
$testCommand
$gitRepo
$author
$license
$isOkay
!

运行脚本时的终端输入和结果:

Terminal input and results when running script:

Project name: name
Version: 1.0.0
Project description: description
Entry point: app.js
Test command: 
Git repository: 
Keywords: 
Author: Author
License: ISC
Is this okay?: yes
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (node) name
version: (1.0.0) 1.0.0
description: description
entry point: (index.js)

就是这样.每次都在入口点退出.

And that's it. It quits at entry point every time.

推荐答案

我不知道为什么这不起作用,但是程序可以直接使用stdin来读取用户输入.npm可能属于此类.

I don't know exactly why this does not work, but a program can read user input without directly using stdin. It is possible that npm falls into this category.

在这种情况下,传递文本提示的解决方案是使用 expect 命令.

For such cases the solution for passing text to prompt is to use the expect command.

read -p "Project name: " name;
read -p "Version: " version;
read -p "Project description: " description;
read -p "Entry point: " entryPoint;
read -p "Test command: " testCommand;
read -p "Git repository: " gitRepo;
read -p "Keywords: " keywords;
read -p "Author: " author;
read -p "License: " license;
read -p "Is this okay?: " isOkay;


/usr/bin/expect <<!

spawn npm init
expect "package name:"
send "$name\n" 
expect "version:"
send "$version\n" 
expect "description:"
send "$description\n" 
expect "entry point:"
send "$entryPoint\n" 
expect "test command:"
send "$testCommand\n" 
expect "git repository"
send "$gitRepo\n" 
expect "keywords:"
send "$keywords\n" 
expect "author:"
send "$author\n" 
expect "license:"
send "$license\n" 
expect "Is this ok?"
send "$isOkay\n"
expect eof
!
echo "DONE!"

这篇关于Shell脚本输入不适用于npm init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆