gpt4 book ai didi

linux - 为什么 cat 0>file 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:29 24 4
gpt4 key购买 nike

在 Unix 中,我知道 0 1 和 2 代表 stdin stdout 和 stderr。

据我了解,命令 cat 意思是“连接”可以连接不同的文件。

例如,cat file>&1可以连接file和stdout,箭头表示从file重定向到stdout ,所以我们可以从标准输出的终端看到文件的内容。

但是,我不明白为什么下面的命令不起作用:
cat 0>文件

我认为这个命令应该有效,因为它意味着连接标准输入和文件,并从标准输入重定向到文件
但是它不起作用,我得到一个错误:

cat: input error on standard input: Bad file number

我以为cat > filecat 0>file是完全一样的,就像cat filecat file >&1 完全一样,但我好像错了...

令我惊讶的是,cat 1>filecat > file 是一样的。为什么?

最佳答案

语法 0>filestdin 重定向到一个文件中(如果这有意义的话)。然后 cat 尝试从 stdin 读取并得到 EBADF 错误,因为 stdin 不再是输入流。

EBADF - fd is not a valid file descriptor or is not open for reading.

请注意,重定向(< 和 >)由 shell 处理,cat 看不到 0>file 位。

关于linux - 为什么 cat 0>file 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com