Node.js产生'echo $(python
我想使用node.js来生成echo $(python --version)
,如果我将它放入我的终端它没有问题,我得到类似的东西
Python 2.7.12
但是,如果我使用以下代码:
var spawn = require('child_process').spawn
var child = spawn('echo', ['$(python --version)'])
child.stdout.on('data', function(b){
console.log(b.toString())
})
我只是把字符串文字回复给我:
$(python --version)
如何将参数转义为正确生成,以便获得正确的输出。
编辑:我特别想使用spawn和echo,我想知道是否有正确的转义spawn参数的解决方案...
回答如下:这应该可以帮到你。
var exec = require('child_process').exec;
exec('python --version', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
根据评论中的要求编辑:
var exec = require('child_process').exec;
exec('echo "Output goes here"', function(error, stdout) { //Replace echo with any other command.
console.log(stdout);
});
输出:输出到这里。
可能要检查一下:How do I escape a string for a shell command in node?