如何从 Deno 运行任意 shell 命令?

2023-12-22

我想从 Deno 运行任意 bash 命令,就像使用child_process在节点中。这在 Deno 中可能吗?


Deno 1.28.0 添加了一个新的 API 来运行 shell 命令:Deno.Command https://deno.land/api?s=Deno.Command (--allow-run需要许可)

let cmd = new Deno.Command("echo", { args: ["hello world"] });
let { code, stdout, stderr } = await cmd.output();
// stdout & stderr are a Uint8Array
console.log(new TextDecoder().decode(stdout)); // hello world

更高级的用法:

const command = new Deno.Command(Deno.execPath(), {
  args: [
    "eval",
    "console.log('Hello World')",
  ],
  stdin: "piped",
  stdout: "piped",
});
const child = command.spawn();

// open a file and pipe the subprocess output to it.
child.stdout.pipeTo(
  Deno.openSync("output", { write: true, create: true }).writable,
);

// manually close stdin
child.stdin.close();
const status = await child.status;

const s = await c.status;
console.log(s);

该 API 取代了已弃用的Deno.run


旧答案:

为了运行 shell 命令,您必须使用Deno.run,这需要--allow-run权限。

有一个持续讨论 https://github.com/denoland/deno/issues/3378 to use --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 to "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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Deno 运行任意 shell 命令? 的相关文章

  • rust axum 项目实践 deno js运行时集成

    rust axum使用deno runtime 使用场景 xff1a 例如在创建订单的过程中 订单创建完成之后 需要根据订单的金额 和订单下单数量进行 1 xff1a 增加用户积分 2 xff1a 赠送优惠券 3 xff1a 消息推送 对于
  • Deno推出高性能键值数据库Deno KV

    出品 OSC开源社区 xff08 ID xff1a oschina2013 Deno 团队宣布推出 Deno KV xff0c 并称其是具备强一致性的键值数据库 xff0c 支持在全局范围复制以在全球 35 个地区实现低延迟读取 Deno
  • 如何在 DENO 中使用 npm 模块?

    德诺超级酷 我早上看到了 现在想迁移到 deno 我试图将现有的 Nodejs 脚本移至 deno 任何人都可以帮助我如何在 deno 中使用 npm 模块 我需要 esprima 模块 这个有包https github com denol
  • 带扩展名的 TypeScript 导入

    您可能听说过 Deno 它是一个新的 TypeScript 运行时 Deno 和普通 TypeScript 之间的一个主要区别是您必须在 import 语句中包含文件扩展名 例如 import foo from bar ts 我想编写与 D
  • 如何让网络工作者同时执行多项任务?

    我试图让 Web Worker 管理其状态 同时服务多个异步请求 工人 ts 文件 let a 0 this is my worker s state let worker self as unknown as Worker worker
  • 为什么第一个函数调用的执行速度比所有其他顺序调用快两倍?

    我有一个自定义 JS 迭代器实现和用于测量后一个实现的性能的代码 const ITERATION END Symbol ITERATION END const arrayIterator array gt let index 0 retur
  • 如何访问 Oak/Deno 中的表单主体

    我用的是橡木 德诺 我有一个从提供的 ejs 文件提交的表单 如何访问表单主体 当我将其记录到控制台时 它会打印 type form value URLSearchParamsImpl 帖子处理程序如下所示 router post add
  • 如何从 Deno 运行任意 shell 命令?

    我想从 Deno 运行任意 bash 命令 就像使用child process在节点中 这在 Deno 中可能吗 Deno 1 28 0 添加了一个新的 API 来运行 shell 命令 Deno Command https deno la
  • 无法在 Ubuntu 18.04 中安装 Deno

    我正在尝试在我的 Ubuntu 机器上安装 Deno 但我不能 Ubuntu 版本是 18 04 我正在使用这个命令 curl fsSL https deno land x install install sh sh 它是由 Deno 官方
  • Deno 顶级等待

    正在阅读homepage https deno land 新的 JS 运行时 deno 我看到了下面的代码 import serve from https deno land email protected cdn cgi l email
  • Deno 允许所有权限

    我经常发现自己在使用 Deno 时至少输入两到三个权限选项 deno run allow net allow read allow env app ts 有一种方法可以逃避显式权限 您可以使用 allow all或短选项 A允许所有权限 请
  • deno 捆绑失败。类型“ReadableStream”上不存在属性“getIterator”

    使用捆绑包运行 deno 失败并出现以下错误 error TS2339 ERROR Property getIterator does not exist on type ReadableStream
  • 使用 Deno 编译时如何使用 lib.dom.d.ts 中的类型?

    我使用 Deno 编译一些 TypeScript 然后将其作为网页的一部分提供 以便它在浏览器端运行 我正在尝试在客户端使用画布元素 为此我需要类似的类型CanvasRenderingContext2D or CanvasGradient
  • 测试用例在 deno 上泄漏异步操作

    我从 Drash 下载了示例应用程序 https github com drashland eno drash https github com drashland deno drash deno run allow run allow r
  • Node.js Buffer.from(string) 的 Deno 等价物是什么

    如何将字符串转换为缓冲区 我试过 Uint8Array from hello world 但它不起作用 相当于Buffer from Hello World is const encoder new TextEncoder const bu
  • 未捕获的 InvalidData:数据与未标记的枚举 ArgsEnum 的任何变体都不匹配

    我对 Deno 很感兴趣 所以我正在尝试一下 找到了有关构建 REST API 的教程here https blog logrocket com creating your first rest api with deno and post
  • 在哪里可以看到 deno 下载的软件包?

    我是新来的deno目前正在探索 deno 的最小可行项目 我想要喜欢 npm 它会下载文件夹内的 npm 包node modules 同样我想看到迪诺包在一个目录中 在我当前的项目中 我没有看到任何下载的包 请建议我在哪里寻找 deno 软
  • 如何在 Deno 中下载大文件?

    我试图下载一个 10GB 的文件 但只有 4GB 保存到磁盘上 而且内存增长了很多 const res await fetch https speed hetzner de 10GB bin const file await Deno op
  • deno 安装和 deno 编译之间有什么区别?

    If I do deno help表明 编译 将脚本编译成独立的可执行文件 install 将脚本安装为可执行文件 这两个命令有什么区别 Command Output Self contained 1 Deno runtime integr
  • 使用 Denon 时如何避免权限问题

    我正在运行 denon 它就像节点中的 nodemon 但即使我手动指定了相关标志 特别是 allow net flag 如何使用 Denon 运行我的应用程序 这样我就不必不断重新启动 如果不知道确切的错误 很难给你正确的答案 但是den

随机推荐