admin管理员组文章数量:1334924
I have this NodeJS script:
var util = require('util'),
process = require('child_process'),
ls = process.exec('test.sh');
ls.stdout.on('data', function (data) {
console.log(data.toString());
ls.stdin.write('Test');
});
and this shell script:
#!/bin/bash
echo "Please input your name:";
read name;
echo "Your name is $name";
I tried to run the NodeJS script and it stucked at "Please input your name:". Does anyone know how to send an input from NodeJS script to the shell script ?
Thanks
I have this NodeJS script:
var util = require('util'),
process = require('child_process'),
ls = process.exec('test.sh');
ls.stdout.on('data', function (data) {
console.log(data.toString());
ls.stdin.write('Test');
});
and this shell script:
#!/bin/bash
echo "Please input your name:";
read name;
echo "Your name is $name";
I tried to run the NodeJS script and it stucked at "Please input your name:". Does anyone know how to send an input from NodeJS script to the shell script ?
Thanks
Share Improve this question edited Apr 11, 2017 at 5:53 Rudy Lee asked Jun 25, 2014 at 12:50 Rudy LeeRudy Lee 4385 silver badges18 bronze badges 02 Answers
Reset to default 5You will have to say something like this:
ls.stdin.write('test\n');
OR
you can inherit standard streams if you want input from user using spawn
.
like this:
var spawn = require('child_process').spawn;
spawn('sh',['test.sh'], { stdio: 'inherit' });
Did you try adding '\n'
to the end of your input (e.g. ls.stdin.write('Test\n');
) to simulate pressing return/enter?
Also, you want process.spawn
, not process.exec
. The latter does not have a streaming interface like you are using, but it instead executes the mand and buffers stdout and stderr output (passing it to the callback given to process.exec()
).
本文标签: javascriptSend user input from NodeJS to shell scriptStack Overflow
版权声明:本文标题:javascript - Send user input from NodeJS to shell script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375511a2463080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论