简体   繁体   English

Shell 脚本无法将文件数据传递给 shell 输入

[英]Shell script can not pass file data to shell input

cal April 2012 | cat > t | cat < t | more

Why does it showing nothing?为什么它什么都不显示? Why isn't it showing为什么不显示

     April 2012       
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30  

| (anonymous pipe) connects stdout ( 1 ) of the first process with stdin ( 0 ) of the second. (匿名管道)将第一个进程的 stdout ( 1 ) 与第二个进程的 stdin ( 0 ) 连接起来。 After redirecting the output to a file, there is no stdout left, so there's nothing to pipe. Also, cat | cat < file将 output 重定向到文件后,标准输出已不存在,因此 pipe 也没有任何内容。另外, cat | cat < file cat | cat < file does not really make sense, it gets two inputs connected to stdin (at least with bash, redirection comes later and "wins": echo uiae | cat <somefile will output the content of somefile) cat | cat < file没有真正意义,它有两个输入连接到标准输入(至少对于 bash,重定向稍后出现并且“获胜”: echo uiae | cat <somefile将 output somefile 的内容)

If you want to display output of a command and, at the same time, write it to the file, use the tee binary.如果要显示命令的 output,同时将其写入文件,请使用tee二进制文件。 It writes to a file, but still writes to stdout它写入文件,但仍写入标准输出

cal April 2012 | tee t | more
cat t # content of the above `cal` command

Because that first cat > t sends all its output to a file called t , leaving no more for the pipeline.因为第一个cat > t将其所有 output 发送到一个名为t文件,不再为管道留下更多。

If your intent is to send it to a file and through more to the terminal, just use:如果您打算将其发送到文件并通过more发送到终端,只需使用:

cal April 2012 | tee t | more

This | cat < t| cat < t | cat < t construct is very strange and I'm not even sure if it would work. | cat < t构造非常奇怪,我什至不确定它是否有效。 It's trying to connect two totally different things to the standard input of cat and certainly unnecessary.它试图将两个完全不同的东西连接到cat的标准输入,这当然是不必要的。

this works for me if there's no existing file named t in the current directory.如果当前目录中没有名为t的现有文件,这对我有用。 I'm using bash on Ubuntu Oneiric.我在 Ubuntu Oneiric 上使用 bash。

$ cal April 2012 | cat > t | cat < t | more
 April 2012       
 Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
 15 16 17 18 19 20 21  
 22 23 24 25 26 27 28  
 29 30
$ cal April 2012 | cat > t | cat < t | more
$ rm t
$ cal April 2012 | cat > t | cat < t | more
 April 2012       
 Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
 15 16 17 18 19 20 21  
 22 23 24 25 26 27 28  
 29 30

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

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