[CentOS] [OT] bash here documents

Tony Mountifield tony at softins.co.uk
Tue Jun 25 10:07:05 UTC 2013


In article <20130624172106.61M6U.130379.root at cdptpa-web19-z02>,
Steve <zephod at cfl.rr.com> wrote:
> 
> ---- Tony Mountifield <tony at softins.co.uk> wrote: 
> > In article <20130624135817.10QZO.129278.root at cdptpa-web19-z02>,
> > Steve <zephod at cfl.rr.com> 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. 
> > 
> > You should be testing the return value from std::cin.getline() for
> > end-of-file. You are not testing it for any error. Always test return values.
> My program is a quick-and-dirty way to simulate the actual program I am trying to use.
> I have no access to the code and all I know about it is that when I enter a command, it responds and the only way to kill it is with <ctrl>-C.

Ah, I guess I missed the significance of the word "suppose"...

> > In your script, the end of the here document will cause your program to see
> > end-of-file (this is NOT the string "EOF" - that's just the way you denote
> > the end of the here doc).
> I am aware of that. I'm trying to find a way to send the program <Ctrl>-C from the script.

I don't believe that is possible. ^C is not part of the data stream read by the program.
What happens is that when you press ^C, your terminal driver notices it, swallows it,
and sends an INTR signal to the process attached to the terminal. It is that INTR
signal that kills the program, if it has not been written to capture and handle that signal.
When reading the data from a file or here document, that data doesn't go via the terminal driver.

> > And you really ought to find a forum or mailing list where this kind of
> > question is ON-topic. Flagging the subject OT isn't a magic permission
> > to post anything you like!
> I agree, I really ought to.

Cheers
Tony
-- 
Tony Mountifield
Work: tony at softins.co.uk - http://www.softins.co.uk
Play: tony at mountifield.org - http://tony.mountifield.org



More information about the CentOS mailing list