尝试制作一个CLI类型的程序-等待用户输入。在Cygwin,剧本就会退出。就在脚本process.stdin.resume()中
似乎可以在Linux上工作。也可以在Windows命令行上工作。
我想是关于Cygwin的“终点站”。
发布于 2014-02-07 23:56:29
症状是Cygwin process.stdin.on('data', cb);和process.stdin.on('end', cb);没有被调用。
我为它找到了一个解决办法
版本
> 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也许它适用于更新的节点,我不知道,我需要使用这个版本。
c:\test\myecho.js
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上试试
c:\test>echo hello | node myecho
Starting up
Waiting for data
.
Got
hello
c:\test>_让我们在Cygwin试试
/cygdrive/c/test$ echo hello | node myecho
Starting up
Waiting for data
/cygdrive/c/test$ _让我们再试一次“在Cygwin”
/cygdrive/c/test$ cmd /c 'echo hello | node myecho'
Starting up
Waiting for data
.
Got the full thing :)
hello
/cygdrive/c/test$ _好了,现在让它变的别扭!
/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从文件中读取。
便携性
如果有人想要编写一个可移植的脚本,调用需要标准输入的节点应用程序:
#!/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
fihttps://stackoverflow.com/questions/12218542
复制相似问题