Shell script can not pass file data to shell input
Question
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
Solution
|
(anonymous pipe) connects stdout (1
) of the first process with stdin (0
) of the second. After redirecting the output to a file, there is no stdout left, so there's nothing to pipe. Also, 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)
If you want to display output of a command and, at the same time, write it to the file, use the tee
binary. It writes to a file, but still writes to stdout
cal April 2012 | tee t | more
cat t # content of the above `cal` command
OTHER TIPS
Because that first cat > t
sends all its output to a file called t
, leaving no more for the pipeline.
If your intent is to send it to a file and through more
to the terminal, just use:
cal April 2012 | tee t | more
This | cat < t
construct is very strange and I'm not even sure if it would work. It's trying to connect two totally different things to the standard input of cat
and certainly unnecessary.
this works for me if there's no existing file named t
in the current directory. I'm using bash on Ubuntu Oneiric.
$ 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