Re: [OT] bash here documents

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



In article <20130624135817.10QZO.129278.root@cdptpa-web19-z02>,
Steve <zephod@xxxxxxxxxx> 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.

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

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!

Cheers
Tony
-- 
Tony Mountifield
Work: tony@xxxxxxxxxxxxx - http://www.softins.co.uk
Play: tony@xxxxxxxxxxxxxxx - http://tony.mountifield.org
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos




[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux