gpt4 book ai didi

node.js - Bash + Node.js + stdin/stdout 重定向 : error "not a tty"

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:30 38 4
gpt4 key购买 nike

此问题可能是 Windows 特有的。我没有在 Linux 或 Mac 上测试过它。

我使用:

  • Windows 7 64 位
  • Node.js 8.1.3
  • Git for Windows 2.8.1,包括 GNU bash,版本 4.3.42(5)-release

node my-cli.js > foo.txt :错误output is not a tty

node my-cli.js < foo.txt :错误input is not a tty .

最佳答案

发生这种情况是因为默认设置下的 Windows 版 Git 将获取此文件 /etc/profile.d/aliases.sh这会做 alias node="winpty node.exe" , 这是与 node 交互使用所必需的(以及其他程序,如 python ,...)。所以当你调用 node xxx <yyy >zzz ,您的 shell 实际上正在调用 winpty node xxx在引擎盖下

winpty works by starting the winpty-agent.exe process with a new, hidden console window, which bridges between the console API and terminal input/output escape codes. It polls the hidden console's screen buffer for changes and generates a corresponding stream of output.

,但副作用是 stdin 和 stdout 不被识别为 tty。

所以当管道或重定向时,你会想要调用 node二进制本身而不是别名。有一些方法可以实现这一点:

  1. 包装在一个直接调用node的shell脚本中因为非交互式 shell 不提供 aliases.sh文件。查看其他答案(shbash 都有效)


  2. 通话 env node my-cli.js > foo.txt或者
    command node my-cli.js > foo.txt

env在默认环境下运行命令,效果同上述方法;同时commandbash用于绕过别名的内置 shell。

  1. 点赞
    \node my-cli.js > foo.txt或者
    'node' my-cli.js > foo.txt或者
    "node" my-cli.js > foo.txt

反斜杠和引号是明确绕过别名的构造。

  1. 使用
    调用 node.exe my-cli.js > foo.txt或者
    /full/path/to/node my-cli.js > foo.txt或者
    relative/path/to/node my-cli.js > foo.txt

别名是node , 不是 node.exe也不path/to/node ,它仍然指向实际的二进制文件。

扩展这些解决方案的一种方法是编写一个包装脚本来检测 piping/redirection (这本身就是一个完全不同的挑战)它将决定使用 winpty还是不是。

关于node.js - Bash + Node.js + stdin/stdout 重定向 : error "not a tty",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45112889/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com