如何从npm脚本导入bash别名?

时间:2023-01-07 23:20:52

I have an alias in my .bashrc for bunyan:

在我的。bashrc中有一个别名:bunyan:

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

This line runs fine in bash:

这一行在bash中运行良好:

$ coffee src/index.coffee | bsh

But if I put the same thing in 'scripts'

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

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

And npm run coffee, it fails:

npm运行咖啡,失败了:

> 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)

So at random I tried putting in || instead of | and it worked. I can't figure out why though. I don't have to escape pipe characters in JSON as far as I know.

所以,我随机地把||换成|,结果成功了。我不知道为什么。就我所知,我不需要转义JSON中的管道字符。

However it doesn't actually pipe the output to the bsh alias.

然而,它实际上并没有将输出传输到bsh别名。

The actual fix is to use "coffee":"coffee src/index.coffee | bunyan -o short" -- get rid of the alias completely.

实际的修复是使用“coffee”:“coffee src/index”。咖啡| bunyan -o short”——完全摆脱别名。

How can I use a bash alias in an npm script?

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

2 个解决方案

#1


2  

You can create a function instead of an alias.

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

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

The export will make it available to children processes.

出口将使其对儿童进程可用。

#2


1  

So I had a whole response typed up about using

所以我有一个关于使用的完整回复

. ~/.bash_aliases && coffee src/index.coffee | bsh

But it turns out that aliases are barely, if at all, supported in bash scripts. From what I have read, aliases are deprecated in favor of functions...

但事实证明,在bash脚本中几乎不支持别名(如果有的话)。根据我所读到的,别名不赞成使用函数……

See this discussion for what convinced me to use functions instead of aliases. I tried for an hour or two to get aliases to work by testing with /bin/bash -c as well as npm run, with no luck. However, using a function as suggested by Diego worked immediately and without problems.

看看这个讨论,说服我使用函数而不是别名。我尝试了一两个小时,用/bin/bash -c和npm运行测试来获得工作的aliases,但是没有运气。然而,按照迭戈的建议,使用一个功能立即生效,没有问题。

I am including this even though the question is already marked as answered in case someone as stubborn as me winds up here from google and decides to try to make aliases work instead of just using a function.

我包含了这个,即使这个问题已经被标记为答案,以防像我这样固执的人从谷歌结束,决定尝试使用别名,而不是仅仅使用函数。

However, I did run into a problem specifically when trying to use this with npm scripts. Even with the export -f, my functions aren't recognized - I still had to manually include the bash_aliases file, and even then, I got an error about the -f option for export.

但是,当我尝试使用npm脚本时,我确实遇到了一个问题。即使有了export -f,我的函数也无法识别——我仍然必须手动地包含bash_aliases文件,即使这样,我仍然得到关于export的-f选项的错误。

In order to actually get this working, I had to take out the function export line and manually include the bash_aliases file...

为了让这个工作起来,我必须取出函数导出行并手动包含bash_aliases文件…

#1


2  

You can create a function instead of an alias.

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

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

The export will make it available to children processes.

出口将使其对儿童进程可用。

#2


1  

So I had a whole response typed up about using

所以我有一个关于使用的完整回复

. ~/.bash_aliases && coffee src/index.coffee | bsh

But it turns out that aliases are barely, if at all, supported in bash scripts. From what I have read, aliases are deprecated in favor of functions...

但事实证明,在bash脚本中几乎不支持别名(如果有的话)。根据我所读到的,别名不赞成使用函数……

See this discussion for what convinced me to use functions instead of aliases. I tried for an hour or two to get aliases to work by testing with /bin/bash -c as well as npm run, with no luck. However, using a function as suggested by Diego worked immediately and without problems.

看看这个讨论,说服我使用函数而不是别名。我尝试了一两个小时,用/bin/bash -c和npm运行测试来获得工作的aliases,但是没有运气。然而,按照迭戈的建议,使用一个功能立即生效,没有问题。

I am including this even though the question is already marked as answered in case someone as stubborn as me winds up here from google and decides to try to make aliases work instead of just using a function.

我包含了这个,即使这个问题已经被标记为答案,以防像我这样固执的人从谷歌结束,决定尝试使用别名,而不是仅仅使用函数。

However, I did run into a problem specifically when trying to use this with npm scripts. Even with the export -f, my functions aren't recognized - I still had to manually include the bash_aliases file, and even then, I got an error about the -f option for export.

但是,当我尝试使用npm脚本时,我确实遇到了一个问题。即使有了export -f,我的函数也无法识别——我仍然必须手动地包含bash_aliases文件,即使这样,我仍然得到关于export的-f选项的错误。

In order to actually get this working, I had to take out the function export line and manually include the bash_aliases file...

为了让这个工作起来,我必须取出函数导出行并手动包含bash_aliases文件…