英文:
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(sh
、bash
)。
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论