0
cat <<lim | sleep 5 | ls >file2 | cat <<lim | grep wow | cat <<lim 

In this example, I don't understand why sleep execution begins at the conclusion of the execution. If any of you have a source or explanation of how this here document works and how he saves the data he gets from input, please share.

dabu
  • 1
  • 2
  • 1
    These are pipes, not buckets. The output of one command are fed directly into the input of the next. Why would `sleep` block the next program from executing? – Thomas Jager May 26 '22 at 11:53
  • & why did every here doc prompt run at first? – dabu May 26 '22 at 11:57
  • 1
    The same reason, which can be restated as: all the commands in the pipeline are started at (approximately) the same time and run in parallel. That's how pipelines work, and how they *must* work. If you want one command to terminate before the next runs, then use `;` or maybe `&&` or `||`, instead of piping commands together. Note also that many of your redirections conflict with the piping, and some of them just don't make sense, so running the commands in parallel is one of the few effects that you're reliably getting at all. – John Bollinger May 26 '22 at 12:27

1 Answers1

0

DON'T DO THIS.

A pipe character basically attaches the stdout of its left operand to the stdin of its right operand. That's what it is for.

Why would you ever do this?

ls >file2 | cat <<lim # 2 redirections out, 2 in!

Putting >file2 on the ls redirects its output to file2.
There is nothing for the pipe. putting a here-doc on cat redirects its input from the HERE portion, so again, it ignores the pipe. Why do you have pipes here at all?

If you just want these things to happen simultaneously, then

ls >file2 & cat <<lim

but I confess I really have no idea what you did want. Were there local files names wow and lim that you expected to change the behavior of the pipelines? I can't tell.

Try looking over these. Hope they help.

To elaborate -
input to any process is by default on fd0.
That is usually the console, e.g., your keyboard.
Redirecting from a file (cat < file) attaches fd0 to that file.
Piping data in is another form of redirection, replacing fd0 -
e.g., echo foo | grep -o oo which should output oo.

here-docs are another form of input redirection.
Command-line redirections for a given stream are all implemented in order, as given. Thus, given echo 2>2,

$: cat # read keyboard; end with CTRL_D, gives it right back.
hi!<CTRL_D>
hi!
$: cat <<< bar # reads from the here-string
bar
$: echo 1 | cat # reads from the pipe
1

But if you start mixing them, whichever redirection happened last will be in effect.

$: echo 1 | cat <<< bar <<END # reads from the here-DOC as last
3
END

3

$: echo 1 | cat <<END <<< bar # reads from the here-STRING as last
3
END

bar

$: echo 1 | cat <<END <<< bar <2 # reads from the file 2 as last
3
END

2

$: echo 1 | cat <<END <<< bar <2 < <(echo 4) # reads the echo 4
3
END

4

These same principles apply for output redirections.

This is the principle that allows shuffling file descriptors...

ls bogus 2 2>&1 >log | tee err >&2

This tries to list the file 2 we used above and a non-existent file named bogus. It will redirect fd2, the error stream, to wherever fd1 is currently going, which defaults to "stdout"; Then it redirects fd1 to the file named log leaving only the error stream going to stdout. Then the pipe character connects stdout to the stdin of the tee program, which duplicates it to the file named err as well as its own stdout, which is redirected back to the stderr stream.

The result is that the 2 reports to log, but the error for bogus reports both to the screen (on stderr) and to the err file.

But don't do this unless you KNOW WHY and exactly what you are doing.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • 1
    I'm attempting to write my shell, thus I tried to emulate the behaviour of the bash shell. I observed the shell run all the heredoc and he received the data after the sleep command I was wondering how here doc functions with pipe and files and how he saves the data I know he builds a temporary file for each here doc My quote is why all here docs follow each other. – dabu May 26 '22 at 18:06