Nodejs process.stdin.resume()在Cygwin中不工作 [英] Nodejs process.stdin.resume() doesn't work in Cygwin
问题描述
尝试创建CLI类型程序 - 等待用户输入。在Cygwin中,脚本刚刚退出。只是这样在脚本process.stdin.resume()
Trying to make a CLI type program - waiting for user input. In Cygwin, the script just exits. Just this in the script process.stdin.resume()
似乎在一个Linux虚拟机上工作。在Windows命令行也。
Seems to work on a Linux VM. Works on Windows Command line also.
Im假设有关Cygwin的终端。
Im assuming 'terminal' stuff regarding Cygwin..
推荐答案
症状是Cygwin process.stdin.on('data',cb);
和 process.stdin.on ('end',cb);
The symptom is that Cygwin process.stdin.on('data', cb);
and process.stdin.on('end', cb);
doesn't get called.
em>
> ver
Microsoft Windows [Version 6.1.7601]
$ uname -a
CYGWIN_NT-6.1 localhost 1.7.28(0.271/5/3) 2014-02-04 16:01 x86_64 Cygwin
$ node --version
v0.8.14
也许它适用于较新的节点,我不知道,我需要使用此版本。
Maybe it works with newer node, I don't know, I need to use this version.
console.error("Starting up");
process.stdin.resume();
process.stdin.setEncoding('utf8');
console.error("Waiting for data");
var inputContents = "";
process.stdin.on('data', function (chunk) {
process.stderr.write(".");
inputContents += chunk.toString();
});
process.stdin.on('end', function () {
console.error("\nGot the full thing :)");
console.log(inputContents);
});
让我们在Windows上尝试它
let's try it on Windows
c:\test>echo hello | node myecho
Starting up
Waiting for data
.
Got
hello
c:\test>_
尝试它在Cygwin
Let's try it in Cygwin
/cygdrive/c/test$ echo hello | node myecho
Starting up
Waiting for data
/cygdrive/c/test$ _
b $ b
让我们再试一次在Cygwin
Let's try it again "in Cygwin"
/cygdrive/c/test$ cmd /c 'echo hello | node myecho'
Starting up
Waiting for data
.
Got the full thing :)
hello
/cygdrive/c/test$ _
b $ b
确定,现在让它变幻想!
Ok, now make it fancy!
/cygdrive/c/test$ cmd /c 'echo hello | node myecho 2>NUL'
hello
/cygdrive/c/test$ cmd /c 'echo hello | node myecho' 2>/dev/null
hello
/cygdrive/c/test$ _
太糟糕了,输入不能来自一个Cygwin文件描述符,但作为一种解决方法,可以将它写入一个文件,然后从中读取 type
例如
Too bad, the input can't come from a Cygwin file descriptor, but as a workaround it's possible to write it to a file and then read it from there with type
for example.
如果有人想编写一个可移植脚本,标准输入:
If someone wants to write a portable script calling a node app expecting standard input:
#!/bin/bash
SCRIPT_DIR='../path/to/scripts' # absolute paths would need a little more playing
if [[ `uname -s` == CYGWIN* ]]; then
cmd /c "type file1 | node ${SCRIPT_DIR//\//\\}\program.js ${BASHVAR}" | grep stuff >file2
else
cat file1 | node ${SCRIPT_DIR}/program.js ${BASHVAR} | grep stuff >file2
fi
这篇关于Nodejs process.stdin.resume()在Cygwin中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!