473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

fgets and EOF in Bash


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)
Nov 15 '05 #1
5 7315
"Steven Woody" <an******************@gmail.com.dont-post-to> wrote in
message news:87************@narke.yellow.line...

i wrote the following bash script:


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

--
Mabden
Nov 15 '05 #2
Mabden wrote:
"Steven Woody" <an******************@gmail.com.dont-post-to> wrote in
message news:87************@narke.yellow.line...
i wrote the following bash script:

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...
Nov 15 '05 #3
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
Nov 15 '05 #4
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

Nov 15 '05 #5
Steven Woody <an******************@gmail.com.dont-post-to> wrote:
| cat - | ./test-eof << EOF


Off-topic in c.l.c, but:
<http://www.catb.org/~esr/jargon/html/U/UUOC.html>.

HTH; HAND.

Richard
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tanel | last post by:
Hello, I need to read a result of the first script (that takes some time to run) from the second script so that the first script doesn't block the second. Here is a short example. do_smth_else()...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
16
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
4
by: melmack3 | last post by:
Hello My PHP script executes many bash/cmd commands. Functions like "exec()" or "system()" cause that new bash/cmd session is started, the command is executed and the session is closed....
6
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.