我有一点空闲时间,所以我决定用子进程重写JavaScript中的所有bash脚本(NodeJS - ES6)。一切都很顺利,直到我想让用户输入自动化。有什么方法可以确定一个nodejs childprocess是想要输入还是只是发送反馈?
是的,你可以自动执行用户输入。但有一个问题 - 您无法确定给定的data
事件是反馈还是输入请求。至少我找不到办法做到这一点。
所以基本上你可以这样做:
// new Spawn.
let spawn = require('child_process');
// new ufw process.
let ufw = spawn('ufw', ['enable']);
// Use defined input.
ufw.stdin.setEncoding('utf-8');
ufw.stdout.pipe(process.stdout);
ufw.stdin.write('y\n');
// Event Standard Out.
ufw.stdout.on('data', (data) => {
console.log(data.toString('utf8'));
});
// Event Standard Error.
ufw.stderr.on('data', (err) => {
// Logerror.
console.log(err);
});
// When job is finished (with or without error) it ends up here.
ufw.on('close', (code) => {
// Check if there were errors.
if (code !== 0) console.log('Exited with code: ' + code.toString());
// End input stream.
ufw.stdin.end();
});
上面的例子中工作完全正常。但有两两件事给我一个头痛:
会直到需要
ufw.stdin.write('y\n');
等待,如果我有多个输入时会发生什么?例如'是','是','否'。我需要写3行stdin.write()
吗?是不是我使用
ufw.stdin.write('y\n');
有点混淆的位置?在我的提示提出输入请求后,我想我需要输入信息,所以我决定更改我的代码,以便我的stdin.write()
能够在正确的时间运行,理解正确吗?但是,只有在stdout.on('data', callback)
事件中检查“正确”时间的唯一方法。这使得认为有点困难,因为我需要知道,如果提示aksing供用户输入或不...
这里是我的代码,我认为这是完全错误的:
// new Spawn.
let spawn = require('child_process');
// new ufw process.
let ufw = spawn('ufw', ['enable']);
// Event Standard Out.
ufw.stdout.on('data', (data) => {
console.log(data.toString('utf8'));
// Use defined input.
ufw.stdin.setEncoding('utf-8');
ufw.stdout.pipe(process.stdout);
ufw.stdin.write('y\n');
});
// Event Standard Error.
ufw.stderr.on('data', (err) => {
// Logerror.
console.log(err);
});
// When job is finished (with or without error) it ends up here.
ufw.on('close', (code) => {
// Check if there were errors.
if (code !== 0) console.log('Exited with code: ' + code.toString());
// End input stream.
ufw.stdin.end();
});
我的主要误解是何时使用stdin
进行用户输入(自动)以及将它放在我的代码中以便它在正确的时间使用,例如,如果我有多个输入,例如mysql_secure_installation
。