2005-09-13, 7:01:(+08), Steven Woody:
i wrote the following bash script:
,----[ test-eof.sh ]
| #!/bin/bash
|
| cat - | ./test-eof << EOF
| hello
| world
| EOF
|
`----
and hope it will end and exit when encount the EOF. but it did _not_ until i
pressed an extra newlin.
[...]
A pipeline ends when all the commands end. (see sleep 1 | sleep
12).
In your pipeline, cat output is connected to a pipe, but the
test-eof command doesn't read from that pipe. Instead, it reads
from a temporary file created because of the <<EOF here
document. That means that there's no command reading from the
pipe. cat reads from whatever the standard input was before the
script started as you didn't redirect it.
So, test-eof will end as soon as it reaches the end of the
temporary file. cat will end normally as soon as it sees EOF from its
standard input. But, as there's nobody at the reading end of the
pipe, cat will get a SIGPIPE deadly signal as soon as it tries
to write to it, (when you hit return for instance). The
pipeline will end whenever the latest of the above occurs.
The cat command doesn't make much sense above.
Maybe you meant
cat << EOF | ./test-eof
hello
worlk
EOF
?
But even then, the cat command is of no use unless you prefer
test-eof standard input to be a pipe rather than a regular file.
--
Stéphane