如何将一个星号传递给Deno.Command参数之一?

huangapple go评论67阅读模式
英文:

How can I pass an asterisk to one of the Deno.Command args?

问题

我正在尝试从Deno执行一个shell命令,其中一个参数包含一个星号。示例:

const output = new Deno.Command("cp", { args: ["source/*", "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))

它会产生以下结果:

cp: cannot stat 'source/*': No such file or directory

如何将一个星号传递给Deno.Command的一个参数?

英文:

I am trying to execute a shell command from Deno where one of the args contains an asterisk. Example:

const output = new Deno.Command("cp", { args: ["source/*", "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))

It yields:

cp: cannot stat 'source/*': No such file or directory

How can I pass an asterisk to one of the Deno.Command args?

答案1

得分: 3

Deno使用Rust的std::process::Command

> 请注意,参数不会通过shell传递,而是直接传递给程序。这意味着像引号、转义字符、单词拆分、通配符模式、替代等shell语法都不起作用。

因此,为了使用*,你需要像Glenn Jackman评论的那样启动一个shell(shbash)。

new Deno.Command("sh", { args: ["-c", "cp source/* destination"] }).outputSync()
英文:

Deno uses Rust's std:process::Command

> Note that the argument is not passed through a shell, but given
> literally to the program. This means that shell syntax like quotes,
> escaped characters, word splitting, glob patterns, substitution, etc.
> have no effect.

So in order to use * you'll need to spawn a shell (sh, bash) as Glenn Jackman commented.

new Deno.Command("sh", { args: ["-c", "cp source/* destination"] }).outputSync()

huangapple
  • 本文由 发表于 2023年7月28日 01:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定