use*_*230 9 bash shell node.js
该子进程API可以用来执行node.js中的shell脚本
我使用child_process.exec(命令[,选项],回调)函数
作为一个选项,exec的用户可以设置shell:'/ path/to/shell'字段来选择要使用的shell.(默认为'/ bin/sh')
将选项设置为{shell:'/ bin/bash'}不会使exec使用bash来破坏命令.
我通过发出打印"/ bin/sh"的命令"echo $ 0"验证了这一点.
如何通过shell选项将bash与child_process.exec一起使用?
(我的目标是在bashrc中使用我的路径定义,现在当我尝试使用grunt时,无法找到二进制文件.设置cwd,选项字典中的当前工作目录按预期工作)
-----------------更新,例子
'use strict';
var cp = require('child_process');
cp.exec('echo $0', { shell: '/bin/bash' }, function(err, stdout, stderr){
if(err){
console.log(err);
console.log(stderr);
}
console.log(stdout);
});
Run Code Online (Sandbox Code Playgroud)
输出:
/bin/sh
Run Code Online (Sandbox Code Playgroud)
which bash
Run Code Online (Sandbox Code Playgroud)
打印:/ bin/bash
可能在您的设置中或在代码中错误地传递了选项。由于您没有发布代码,因此很难分辨。但是我能够执行以下操作,并且可以正常工作(使用节点4.1.1):
"use strict";
const exec = require('child_process').exec
let child = exec('time',{shell: '/bin/bash'}, (err, stdout, stderr) => {
console.log('this is with bash',stdout, stderr)
})
let child2 = exec('time',{shell: '/bin/sh'}, (err, stdout, stderr) => {
console.log('This is with sh', stdout, stderr)
})
Run Code Online (Sandbox Code Playgroud)
输出将是:
this is with bash
real 0m0.000s
user 0m0.000s
sys 0m0.000s
This is with sh /bin/sh: 1: time: not found
Run Code Online (Sandbox Code Playgroud)
我用作time
命令,因为它是bash拥有的命令,而sh没有。我希望这有帮助!
归档时间: |
|
查看次数: |
5468 次 |
最近记录: |