fgets and EOF in Bash

S

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.

the test-eof program is a simple c code:

,----[ test-eof.c ]
|
| int main(int argc, char *argv[])
| {
| char buf[1024];
| while (fgets(buf, 1024, stdin) != NULL)
| ;
| return 0;
| }
`----


would anyone please tell me why and how to fix it? thanks.


--
steven woody (id: narke)

Jesse: I heard this story once about when the Germans were occupying
Paris and they had to retreat back. They wired Notre Dame to blow,
but they had to leave one guy in charge of hitting the switch. And
the guy, the soldier, he couldn't do it. You know, he just sat
there, knocked out by how beautiful the place was. And then when
the allied troops came in, they found all the explosives just
lying there and the switch unturned, and they found the same thing
at Sacre Couer, Eiffel Tower. Couple other places I think...
Celine: Is that true?
Jesse: I don't know. I always liked the story, though.

- Before Sunset (2004)
 
J

J. F. Cornwall

Mabden said:
What is a "bash"? It is not in my K&R index.

A "bash" is what anyone gets who dares ask a question that deviates from
the incredibly narrow topicality focus in comp.lang.c newsfloop...
 
S

Stephane CHAZELAS

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.
 
U

Umberto

Just use

../test-eof <<EOF
hello
world
EOF

Why are you adding cat if it's not needed?
As already pointed out by others, your redirection was misplaced.

Umberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top