乐闻世界logo
搜索文章和话题

How to get the output of a command in Deno?

1个答案

1

In Deno, you can use the Deno.run method to execute system commands and capture their output. The Deno.run method creates a new process and executes the specified command. To capture the command's output, you need to redirect standard output (stdout) to a pipe and then read the data from this pipe. Here is a specific example:

typescript
// Create a Deno process to run the command const process = Deno.run({ cmd: ["echo", "Hello World!"], // This is the command and its arguments stdout: "piped", // Redirect standard output to a pipe }); // Read output data from the pipe const output = await process.output(); process.close(); // Close the process // Convert output data from Uint8Array to a string const outStr = new TextDecoder().decode(output); console.log(outStr);

In this example, we use the echo command as a demonstration, which prints content to standard output. We set stdout to "piped" to capture this output. Then, we use process.output() to asynchronously read the output content and finally convert it from binary format to a string.

This method is suitable for executing any external command that requires capturing its output. You can modify the cmd array as needed to execute different commands.

2024年8月8日 03:06 回复

你的答案