最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Node.js产生'echo $(python

运维笔记admin14浏览0评论

Node.js产生'echo $(python

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?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论