[英]Why does a working shell script stop executing when executed from shelljs?
I've been working on a shell script to automatically set up the directory system for a basic web document, as an attempt to keep my projects a bit more consistently organized. 我一直在研究一个shell脚本,以自动为基本的Web文档设置目录系统,以使我的项目更加一致。
#!/bin/bash
if [ -e $1 ]; then
echo $1 'already exists!'
exit
else
echo 'Generating' $1 '. . .'
mkdir $1
cd $1
mkdir 'html'
mkdir 'scripts'
touch 'scripts/app.js'
mkdir 'style'
touch 'style/style.css'
touch 'index.html'
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>' > 'index.html'
read -p 'Do you want a git repo? [Y/N]' response
if [ $response = 'Y' ] || [ $response = 'y' ]; then
echo 'Generating git repo . . .'
git init
fi
fi
if [ $# -eq 1 ]; then
read -p 'Do you want a package.json? [Y/N] ' response
if [ $response = 'Y' ] || [ $response = 'y' ]; then
npm init
fi
else
npm init
shift
echo 'installing' $*
npm install $*
touch '.gitignore'
echo '/node_modules' > '.gitignore'
fi
echo 'Your web app has been generated!'
echo 'You can find it at'
pwd
read -p 'Would you like to push your first commit now? [Y/N]'
if [ $response = 'Y' ] || [ $response = 'y' ]; then
git add .
git commit -m 'initial commit'
read -p 'Please enter the url to your git (.git):' url
git remote add origin $url
git push -u origin master
fi
read -p 'Would you like to open VSCode? [Y/N] ' response
if [ $response = 'Y' ] || [ $response = 'y' ]; then
code .
fi
read -p 'Would you like to start live-server? [Y/N] ' response
if [ $response = 'Y' ] || [ $response = 'y' ]; then
live-server
fi
exit
The shell script works when I run it natively (ex: ./webapp.sh myapp lodash
). 当我在本地运行时,shell脚本有效(例如:
./webapp.sh myapp lodash
)。 It will create the directories and, if prompted, initialize a git repo and install dependencies, open VSCode and launch live-server. 它将创建目录,并在出现提示时初始化git repo并安装依赖项,打开VSCode并启动实时服务器。 That's great and all.
太好了,所有。
But then I had a thought, if I refined this, maybe others would want to use it and it might be useful as an NPM package (granted, it's nowhere near ready for an NPM publication). 但是后来我想到,如果我对此进行完善,也许其他人会想使用它,并且它可能作为NPM软件包很有用(当然,它距离NPM出版物还差得很远)。 Instead of rewriting the whole thing in Node, I decided to just use
shelljs
: 我决定只使用
shelljs
,而不是重写Node的全部shelljs
:
const shelljs = require('shelljs');
const args = process.argv.slice(2).reduce((accum, current)=> {
return accum + current + ' ';
}, ' ').trim();
shelljs.exec(`./webapp.sh ${args}`);
Using Javascript it would be called node ./webapp.js myapp lodash express
(as an example.) In this case, shelljs should execute ./webapp.sh myapp lodash express
(where myapp is the name of the app and any following arguments are libraries to be installed). 使用Javascript将其称为
node ./webapp.js myapp lodash express
(作为示例。)在这种情况下,shelljs应该执行./webapp.sh myapp lodash express
(其中myapp是应用程序的名称,以下任何参数是库)。 But what happens is after setting up the initial directories and echoing Generating myap ...
it just hangs. 但是,在设置初始目录并回显
Generating myap ...
之后,会发生什么Generating myap ...
它只是挂起。 It doesn't run the read -p 'Do you want a git repo'
line. 它不会运行
read -p 'Do you want a git repo'
行。
Is this a problem with my version of bash (3.2) not matching that of Shelljs maybe? 这是我的bash(3.2)版本与Shelljs版本不匹配的问题吗? I mean, it works fine if I just run it as a shell script, but if I try to run it through shelljs, it hangs.
我的意思是,如果我仅将其作为shell脚本运行,则效果很好,但如果尝试通过shelljs运行,则将挂起。 Is there a solution to this?
有针对这个的解决方法吗? Why would it hang on that
read -p . . .
为什么它将挂在该
read -p . . .
read -p . . .
line only if called through shelljs? 仅通过shelljs调用才行吗?
The repo is here: https://github.com/jckuhl/Web-App-Generator 仓库在这里: https : //github.com/jckuhl/Web-App-Generator
As per the ShellJS FAQ : 根据ShellJS常见问题解答 :
Running interactive programs with
exec()
使用
exec()
运行交互式程序We don't currently support running commands in
exec
which require interactive input.我们目前不支持在
exec
中运行需要交互输入的命令。 The correct workaround is to use the nativechild_process
module:正确的解决方法是使用本机
child_process
模块:child_process.execFileSync(commandName, [arg1, arg2, ...], {stdio: 'inherit'});
Using
npm init
as an example:以
npm init
为例:child_process.execFileSync('npm', ['init'], {stdio: 'inherit'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.