import * as path from "https://deno.land/[email protected]/path/mod.ts";
async function getStdout() {
const dirname = path.dirname(path.fromFileUrl(import.meta.url))
const cmd = new Deno.Command("pwsh", { args: ['-c', 'ls', dirname] });
const { _code, stdout, _stderr } = await cmd.output();
const decodedStdout = new TextDecoder().decode(stdout)
console.log(decodedStdout)
}
await getStdout()
This script is simply to get the path of it, run pwsh -c ls $dirname
and print the result back. If running on folder A it works fine, but on folder B it returns nothing. Surely there are files on B; running ls
on B is fine.
这个脚本只是获取它的路径,运行pwsh-c ls$dirname并打印结果。如果在文件夹A上运行,则运行正常,但在文件夹B上,它不返回任何内容。当然有关于B的文件;在B上运行ls就可以了。
However, if I change the command from ls
to echo
, then it works again.
但是,如果我将命令从ls更改为ECHO,则它将再次工作。
What makes this happens?
是什么让这一切发生的?
更多回答
优秀答案推荐
After a sleep I realize I didn't log the code and the stderr. This helps me debug:
睡了一觉后,我意识到我没有记录代码和标准错误。这有助于我调试:
if (code !== 0) {
console.log(new TextDecoder().decode(stderr))
}
In my case, it's because I use spaces in filenames. The fix is to use template literals.
在我的例子中,这是因为我在文件名中使用空格。修复方法是使用模板文字。
Full script:
完整的脚本:
import * as path from "https://deno.land/[email protected]/path/mod.ts";
async function getStdout() {
const dirname = path.dirname(path.fromFileUrl(import.meta.url))
console.log(dirname)
const cmd = new Deno.Command("pwsh", { args: ['-c', 'ls', `'${dirname}'`] });
const { code, stdout, stderr } = await cmd.output();
const decodedStdout = new TextDecoder().decode(stdout)
console.log(decodedStdout)
if (code !== 0) {
console.log(new TextDecoder().decode(stderr))
}
}
await getStdout()
Take a look at Should I avoid using spaces in my filenames?
看看我应该避免在我的文件名中使用空格吗?
更多回答
我是一名优秀的程序员,十分优秀!