deno - 如何从 Deno 运行任意 shell 命令?
问题描述
我想从 Deno 运行任意 bash 命令,就像在 Node.js 中一样child_process
。在 Deno 有可能吗?
解决方案
为了运行 shell 命令,您必须使用Deno.run
,这需要--allow-run
权限。
有一个正在进行的讨论可以用来--allow-all
代替运行子进程
以下将输出到stdout
.
// --allow-run
const process = Deno.run({
cmd: ["echo", "hello world"]
});
// Close to release Deno's resources associated with the process.
// The process will continue to run after close(). To wait for it to
// finish `await process.status()` or `await process.output()`.
process.close();
如果要存储输出,则必须设置stdout/stderr
为"piped"
const process = Deno.run({
cmd: ["echo", "hello world"],
stdout: "piped",
stderr: "piped"
});
const output = await process.output() // "piped" must be set
const outStr = new TextDecoder().decode(output);
/*
const error = await p.stderrOutput();
const errorStr = new TextDecoder().decode(error);
*/
process.close();
推荐阅读
- javascript - 如何在 mongoDB 对象中提供默认值并增加特定值
- c# - 不同几何图形区域的重载方法
- c - C 编程:将整数写入文本文件
- ms-access - MS 访问 - 如何确保 2 个论坛行相互抵消
- c# - 当我在 EF 中使用多对多关系时,我应该如何在视图中显示配方中的所有成分?
- c# - 在 Grid RowDefinition 中添加上边距
- java - 一次提交多个相关任务的线程池的推荐大小
- leaflet - 如何使用 testcafe 测试基于传单的地图?
- ruby-on-rails - Rails 5 在 heroku 上为旧资产提供服务
- node.js - Pusher - 我如何迎接新用户?