From: Ken Bloom Date: 2007-12-24T04:10:04+09:00 Subject: Re: Fairly elementary UNIX question Steckly, Ron wrote: > I've been learning a lot about the .NET architecture and scripting to it > using Visual Basic and C#. I'm trying to write a program right now in > UNIX and I'm a little perplexed. How do I tap into events in UNIX? For > example, if I were to write a script to enter a command into the R > Statistics Program running in Linux, how would I do that? > > Normally, in bash I would type R > > And a new prompt comes up and you can type whatever command you want. > > I'd like to be able to make a simple GUi for it so that its easier for > people to enter commands who are still learning R. But I'm not sure how > to script to a terminal.... > > Am I completely on the wrong track? On UNIX, terminal programs simply have a few open pipes (which behave like files) by default, the standard input, standard output, and standard error. When you start a new program, the calling program is given the other end of each of those pipes. (It's a little more complicated, but various ruby libraries abstract away the compliation and make this process appear exactly as I said it.) When a program runs in a terminal on UNIX, the terminal is the calling process. All the terinal does is display the data that's written to stdout and stderr, and takes keyboard input and sends it to stdin. When Ruby calls a program, Ruby is the calling process, and you can use the pipes however you want. You can get access to the pipes by using popen (built-in) or the Open3 or Open4 libraries. Note that if you want the output of R to appear on Ruby's calling terminal, you have to do a little work to put the output there yourself. --Ken -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/