首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nodejs process.stdin.resume()在Cygwin中不工作

问Nodejs process.stdin.resume()在Cygwin中不工作
EN

Stack Overflow用户
提问于 2012-08-31 15:36:46
回答 1查看 1.8K关注 0票数 2

尝试制作一个CLI类型的程序-等待用户输入。在Cygwin,剧本就会退出。就在脚本process.stdin.resume()中

似乎可以在Linux上工作。也可以在Windows命令行上工作。

我想是关于Cygwin的“终点站”。

EN

回答 1

Stack Overflow用户

发布于 2014-02-07 23:56:29

症状是Cygwin process.stdin.on('data', cb);和process.stdin.on('end', cb);没有被调用。

我为它找到了一个解决办法

版本

代码语言:javascript
复制
> 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

代码语言:javascript
复制
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上试试

代码语言:javascript
复制
c:\test>echo hello | node myecho
Starting up
Waiting for data
.
Got
hello
c:\test>_

让我们在Cygwin试试

代码语言:javascript
复制
/cygdrive/c/test$ echo hello | node myecho
Starting up
Waiting for data
/cygdrive/c/test$ _

让我们再试一次“在Cygwin”

代码语言:javascript
复制
/cygdrive/c/test$ cmd /c 'echo hello | node myecho'
Starting up
Waiting for data
.
Got the full thing :)
hello
/cygdrive/c/test$ _

好了,现在让它变的别扭!

代码语言:javascript
复制
/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从文件中读取。

便携性

如果有人想要编写一个可移植的脚本,调用需要标准输入的节点应用程序:

代码语言:javascript
复制
#!/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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12218542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档