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

标签 linux unix stdout stdin cat

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

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

例如,cat file>&1可以连接file和stdout,箭头表示从file重定向到stdout ,这样我们就可以从终端 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/

相关文章:

regex - 使用 sed linux 交换列

c - Unix C - 将 stdout 重定向到管道,然后返回到 stdout

Linux 上的 rgeos 包安装错误 [R]

unix - JerseyTest Grizzly Web 服务器在 Unix 上的行为

c - 用于读取目录的 K&R 接口(interface) : superfluous DIR structure?

c++ - 通过本地 UNIX 套接字发送双倍

makefile - 如何解开并行 make -j 编译器消息?

c++ - stdin 和 stdout 是否共享任何资源?

c - 如何在干净的环境中从 C 调用 bash?

linux - ps -l <​​proc_id> 输出中的 WCHAN 'finish' 是什么意思?