json - 如何从 npm 脚本通过管道传输到 bash 别名?

标签 json node.js bash

我的 .bashrc 中有一个 bunyan 的别名:

$ alias bsh
alias bsh='bunyan -o short'

这一行在 bash 中运行良好:

$ coffee src/index.coffee | bsh

但是如果我把同样的东西放在'脚本'中

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "coffee":"coffee src/index.coffee | bsh"
  },

npm run coffee,它失败了:

> coffee src/index.coffee | bsh

sh: bsh: command not found
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
  at exports._errnoException (util.js:870:11)
  at WriteWrap.afterWrite (net.js:769:14)

所以我随机尝试放入 || 而不是 | 并且它起作用了。我不知道为什么。据我所知,我不必转义 JSON 中的管道字符。

但是它实际上并没有将输出通过管道传递给 bsh 别名。

实际的解决方法是使用 "coffee":"coffee src/index.coffee | bunyan -o short" —— 完全去掉别名。

如何在 npm 脚本中使用 bash 别名?

最佳答案

您可以创建函数而不是别名。

function bsh() {
    bunyan -o short
}
export -f bsh

导出将使其可供子进程使用。

关于json - 如何从 npm 脚本通过管道传输到 bash 别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308870/

相关文章:

json - Windows Phone 8 JSON

iphone - 像 Sudz 一样从 JSON 转换为 Objective C

linux - 在 ssh 上运行 bash 脚本

linux - Grep 用于多个目录的关键字并移动找到的文件

bash - 如何并发运行但如果失败则退出1?

javascript - 从 mvc.net 获取 AngularJS http 中使用的 url 的可靠方法是什么?

javascript - 将 JSONobj 转换为 Obj 不起作用?

node.js - 将 htaccess 添加到 Ghost 项目

javascript - nodejs异步并行内部异步每个

node.js - 使用nodeJS登录系统