Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

execa执行如何执行多段命令

我需要在nodejs脚本中执行一些shell命令 之前都是使用 child_process 后来尝试 execa 时,执行一些稍微复杂些的命令 无效 比如 我想执行

echo 'hello world!' > /test/hello.txt

使用 child_process 可以正常执行 没有问题 语法为

process.exec(`echo 'hello world!' > /test/hello.txt`,function(){xxx})

使用 execa 执行不报错 但无法写入这个文件 没有效果 语法为

execa('echo', [`'hello world!'`,'>','/test/hello.txt'])
or
execa('echo', [`'hello world!' > /test/hello.txt`])

两种方式均无效

不知是否是我的调用语法或者传参有错误


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
const subprocess = execa('echo', ['hello world!'])
subprocess.stdout.pipe(fs.createWriteStream('/test/hello.txt'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...