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`])

两种方式均无效

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

阅读 4.9k
1 个回答
const subprocess = execa('echo', ['hello world!'])
subprocess.stdout.pipe(fs.createWriteStream('/test/hello.txt'))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题