[CentOS] [OT] bash here documents

Steve zephod at cfl.rr.com
Mon Jun 24 17:36:20 UTC 2013


---- m.roth at 5-cent.us wrote: 
> Steve wrote:
> > Suppose I have this C++ program:
> > #include <iostream>
> > int main (int argc, char** argv)
> > {
> >     while (1)
> >     {
> >         char cmd[80];
> >         std::cin.getline(cmd, 80);
> >         std::cout << "response to " << cmd << std::endl;
> >     }
> > }
> >
> > compiled by: c++ -o junk junk.cpp
> >
> > and I have this bash script:
> > #!/bin/bash
> > ./junk <<EOF
> > blah
> > bleh
> > \cC
> > EOF
> > echo "Something else"
> >
> > When I run the script, the program starts and waits for input forever.
> > I have 2 questions:
> > 1) The "blah" and "bleh" line are not echoed to cout. Why not? Does the
> > here document not send the data to stdin?
> > 2) How do I terminate the program? When run interactively, I use <ctrl>-C.
> 
> I've only done a tiny bit of C++, but a bunch of years of C. First, I
> think the \cC is doing something odd; in vi, I'd have tried typing it in
> with <ctrl-v><ctrl-c>. Second, do you want the output to be "blah bleh"?
I have also tried with <ctrl-V><ctrl-C> and got the same results.
 
> Finally, I am *very* strongly *not* a fan of while. If I have to use it, I
> set *some* limit - in this case, I'd look for an end-of-file, or some word
> you decide for "stop here".
> 
> As the old joke goes, about hunting elephants in Africa, a programmer sets
> up a search patter up, across, up, back, starting in Capetown and ending
> in Cairo.
> 
> The *experienced* programmer puts an elephant in the zoo in Cairo to
> ensure termination of the loop.
My junk program is a quick-and-dirty simulation if the actual program I am trying to use.
I do not have the code for the program and all I know is that it responds to command input and the only way to terminate it is with <ctrl-C>.
If I run my junk program directly, I get:

$ ./junk
blah
response to blah
bleh
response to bleh
^C
$

I want to be able to get the same output from the script AND have the script end and not hang waiting for more input.

Steve



More information about the CentOS mailing list