Running bash script via Node.js - Illegal option -o pipefail

我的未来我决定 提交于 2019-12-13 01:24:34

问题


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"

The error returned is:

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

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

回答1:


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

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

shell: '/bin/bash'


来源:https://stackoverflow.com/questions/36730856/running-bash-script-via-node-js-illegal-option-o-pipefail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!