【问题标题】:shell function can't be called in node无法在节点中调用 shell 函数
【发布时间】:2015-09-09 00:43:52
【问题描述】:

我有这些:

一个shell可执行文件:

function print() {
    echo 1
}

package.json` 文件

{
  "name": "tests",
  "scripts": {
    "test": "./shell.sh"
  }
}

当我在一台 linux 机器上运行 npm test 时,我得到了这个错误

> tests@ test /home/xxxx/test
> ./shell.sh

./shell.sh: 1: ./shell.sh: Syntax error: "(" unexpected
npm ERR! Test failed.  See above for more details.

为什么会这样?有人有一些见识吗?我完全不解。

【问题讨论】:

  • 我没有在脚本中看到 shebang。有没有发布的?

标签: node.js shell


【解决方案1】:

这实际上与 Node 或 npm 没有任何关系,但 shell 脚本缺少 shebang

尝试改为例如

#!/bin/sh
# Note the new line above

function print() {
    echo 1
}

【讨论】:

  • 这行#!/bin/sh 与否,显示相同的错误。 :(这个错误只发生在linux机器上,不在我的mac上。
  • 即在 mac 上,无论有没有 shebang,它都可以工作。但在 linux 上,不管有没有,它都不起作用。
  • @QiangLi 请尽量去掉function关键字,并不是所有的shell都支持。
  • 这是一个很棒的发现。为什么会这样?我不工作的外壳是GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
  • 鉴于您在 shell 方面的丰富经验,您能否帮助查看http://stackoverflow.com/questions/32481734/shell-error-with-bash-bad-substitution-no-closing?这对我来说也是一个令人费解的问题。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2012-11-09
相关资源
最近更新 更多