node.js中的execSync无法正确运行shell命令 [英] execSync in node.js doesn't run shell command properly

查看:40
本文介绍了node.js中的execSync无法正确运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样在node.js中运行 child_process.execSync shell命令:

I'm trying to run a child_process.execSync shell command in node.js like this:

function test() {
    return new Promise(function(resolve, reject) {
        var execSync = require('child_process').execSync;
        var result = execSync('some executable I manually installed', { encoding: 'utf8', silent: true });

        if (result.search('something') > -1) {
            resolve("ok");
        }
        else if (result.search('something else') {
            resolve("nok");
        }
        else {
            reject(Error("ERROR"));
        }
    });
} 

test().then(function(result) {
  console.log(result);
}, function(err) {
  console.log(err);
});

但是,我得到以下错误作为输出.

However, I get the following error as an output.

{ [Error: Command failed: my shell command]
  error: null,
  cmd: 'my shell command',
  file: '/bin/sh',
  args: [ '/bin/sh', '-c', 'my chell command' ],
  options: 
   { silent: true,
     encoding: 'utf8',
     file: '/bin/sh',
     args: [ '/bin/sh', '-c', 'my shell command' ],
     envPairs: 
      [ 'TERM_PROGRAM=Apple_Terminal',
        'SHELL=/bin/bash',
        'TERM=xterm-256color',
        'TMPDIR=/var/folders/k5/qklhpvtj227_5vdnn2d_x6zr0000gn/T/',
        'Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.2zYqrvejoj/Render',
        'TERM_PROGRAM_VERSION=361.1',
        'OLDPWD=/usr/local/bin',
        'TERM_SESSION_ID=8F8D0040-84DE-41F4-84E6-A9481A09CE73',
        'USER=and',
        'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.L5VoA4PiFl/Listeners',
        '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0',
        'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin',
        'PWD=/Users/and/Documents/',
        'XPC_FLAGS=0x0',
        'XPC_SERVICE_NAME=0',
        'SHLVL=1',
        'HOME=/Users/and',
        'LOGNAME=and',
        'LC_CTYPE=UTF-8',
        'DISPLAY=/private/tmp/com.apple.launchd.rwoZybokZk/org.macosforge.xquartz:0',
        '_=/usr/local/bin/node' ],
     stdio: [ [Object], [Object], [Object] ] },
  envPairs: 
   [ 'TERM_PROGRAM=Apple_Terminal',
     'SHELL=/bin/bash',
     'TERM=xterm-256color',
     'TMPDIR=/var/folders/k5/qklhpvtj227_5vdnn2d_x6zr0000gn/T/',
     'Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.2zYqrvejoj/Render',
     'TERM_PROGRAM_VERSION=361.1',
     'OLDPWD=/usr/local/bin',
     'TERM_SESSION_ID=8F8D0040-84DE-41F4-84E6-A9481A09CE73',
     'USER=and',
     'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.L5VoA4PiFl/Listeners',
     '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0',
     'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin',
     'PWD=/Users/and/Documents/',
     'XPC_FLAGS=0x0',
     'XPC_SERVICE_NAME=0',
     'SHLVL=1',
     'HOME=/Users/and',
     'LOGNAME=and',
     'LC_CTYPE=UTF-8',
     'DISPLAY=/private/tmp/com.apple.launchd.rwoZybokZk/org.macosforge.xquartz:0',
     '_=/usr/local/bin/node' ],
  stderr: '',
  stdout: '',
  pid: 16931,
  output: [ null, '', '' ],
  signal: null,
  status: 10 }

我认为这与/bin/sh 有关(顺便说一下,我是OS X),但我不知道如何解决此问题.我可以在终端中运行shell命令,它在带有execSync的节点中将无法运行.另外,当我尝试使用OS X随附的命令(例如 ls )运行 execSync 时,它运行得很好...

I think it has something to do with /bin/sh (I'm OS X by the way), but I don't know how to fix this. I can run the shell command in the Terminal just fine, it just won't work in node with execSync. Also, when I try to run execSync with a command that comes with OS X (like ls), it runs perfectly fine...

有人可以帮忙吗?谢谢!

Can anybody help? Thanks!

推荐答案

如果最后一个exec程序返回错误代码,例如此C代码上的错误代码

if the last exec program return error code for example on this C code

#include <stdio.h>

int main(void)
{

    printf("hi");

    return 1;
}

重点是如果程序不返回0节点child_process抛出错误

the point is if the program not return 0 the node child_process throw error

我的解决方案:

const proc = require('child_process');

// just add " || true" after your command 
var output = proc.execSync('command || true');

对我来说很好:)

这篇关于node.js中的execSync无法正确运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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