简体   繁体   English

为什么 cat 0>file 不起作用

[英]Why cat 0>file doesn't work

In Unix, I know that 0 1 and 2 represent stdin stdout and stderr.在 Unix 中,我知道 0 1 和 2 代表 stdin stdout 和 stderr。

As my understanding, the command cat meaning "concatenate" can concatenate different files.据我了解,命令cat意思是“连接”可以连接不同的文件。

For example, cat file>&1 can concatenate the file and the stdout and the arrow means the redirection from the file to the stdout, so we can see the content of the file from the terminal which is stdout.例如cat file>&1可以连接file和标准输出,箭头表示从file重定向到标准输出,所以我们可以从标准输出终端看到file的内容。

But, I don't understand why the command below doesn't work:但是,我不明白为什么下面的命令不起作用:
cat 0>file

I think this command should work, because it means that to concatenate the stdin and the file and do the redirection from the stdin to the file .我认为这个命令应该可以工作,因为它意味着连接 stdin 和file并从 stdin 重定向到file
However it doesn't work and I get an error:但是它不起作用,我收到一个错误:

cat: input error on standard input: Bad file number猫:标准输入上的输入错误:文件编号错误

I thought that cat > file and cat 0>file are exactly the same, just like cat file and cat file>&1 are exactly the same, but it seems I'm wrong...我以为cat > filecat 0>file是一模一样的,就像cat filecat file>&1完全一样,但好像我错了...

To my surprise, cat 1>file and cat > file are the same.令我惊讶的是, cat 1>filecat > file是相同的。 Why?为什么?

Syntax 0>file redirects stdin into a file (if that makes sense). 语法0>filestdin重定向到文件中(如果有意义的话)。 Then cat tries to read from stdin and gets EBADF error because stdin is no longer an input stream. 然后cat尝试从stdin读取并获取EBADF错误,因为stdin不再是输入流。

EBADF - fd is not a valid file descriptor or is not open for reading. EBADF - fd不是有效的文件描述符,或者不能读取。

Note that redirections (< and >) are handled by the shell, cat does not see 0>file bit. 请注意,重定向(<和>)由shell处理,cat不会看到0>file位。

In general, cat prints the contents of a file or from the stdin.通常, cat打印文件的内容或来自标准输入的内容。 If you don't provide a file and redirect the stdin to a file, then cat doesn't have any input to read from.如果您不提供文件并将标准输入重定向到文件,则cat没有任何输入可供读取。

The correct form would be: cat <&0 > file.txt , that is:正确的形式是: cat <&0 > file.txt ,即:

  • <&0 redirect stdin as input for cat (similar to cat < some-file.txt ) <&0将 stdin 重定向为cat输入(类似于cat < some-file.txt
  • > file.txt redirect the output of cat to file.txt > file.txtcat的输出重定向到file.txt

This works both for:这适用于:

  • echo "hello" | cat <&0 > file.txt echo "hello" | cat <&0 > file.txt , that is, piping the output of some command echo "hello" | cat <&0 > file.txt ,即管道输出某些命令
  • cat <&0 > file.txt as stand alone and you type directly on the console (quit with Ctrl-C) cat <&0 > file.txt作为独立文件,您可以直接在控制台上输入(使用 Ctrl-C 退出)

As a side note:作为旁注:

# This works (no errors) as cat has a file in input, but:
# 1. the contents of some-file-with-contents.txt will be printed out
# 2. file.txt will not contain anything
cat some-file-with-contents.txt 0>file.txt

# This works (no errors) as cat has a file in input, but:
# 1. the contents of some-file-with-contents.txt will be printed out
# 2. file.txt will not contain anything
# 3. copy.txt will have the contents of some-file-with-contents.txt
cat some-file-with-contents.txt 0>file.txt 1>copy.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM