通过Node.js运行bash脚本-非法选项-o pipefail [英] Running bash script via Node.js - Illegal option -o pipefail

查看:156
本文介绍了通过Node.js运行bash脚本-非法选项-o pipefail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 child_process.exec()通过Node.js执行bash脚本.但是,它在文件的第二行上爆炸了:

I am trying to exec a bash script via Node.js using child_process.exec(). However it is blowing up on the second line of the file:

#!/usr/bin/env bash
set -eo pipefail; [[ $TRACE ]] && set -x

echo "we are here"

返回的错误是:

/bin/sh: 2: set: Illegal option -o pipefail

为什么会这样?当我手动运行脚本(而不是从Node运行)时,它可以正常运行.这是Node.js代码:

Why is this happening? When I run the script manually, not from Node it works fine. Here is the Node.js code:

var child = child_proc.exec(bashScript, {
    env: _.extend(process.env, {
        'LB_HOST': config.loadBalancers.lb1
    }),
    timeout: 0
});

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

推荐答案

默认情况下,调用 child_process.exec()时,它会在Ubuntu上使用/bin/sh 实际上是指向/bin/dash 的符号链接.Dash是bash的精简版本,我猜不支持:

By default when you invoke child_process.exec() it uses /bin/sh which on Ubuntu is actually a symbolic link pointing to /bin/dash. Dash is a stripped down version of bash and I guess does not support:

set -eo pipefail; [[ $TRACE ]] && set -x

在Node.js child_proc.exec()中添加 shell 选项可解决此问题:

Adding the shell option to the Node.js child_proc.exec() fixes this:

shell: '/bin/bash'

这篇关于通过Node.js运行bash脚本-非法选项-o pipefail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆