1
我想在這樣的Node.js運行child_process.execSync
shell命令:execSync在node.js中不運行shell命令正確
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);
});
不過,我得到了下面的錯誤作爲輸出。
{ [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
時,它運行得非常好......
任何人都可以幫忙嗎? 謝謝!