Re: exec_group/3 and redirecting its output to a file

From: Warwick Harvey <>
Date: Fri 01 Nov 2002 01:57:47 PM GMT
Message-ID: <>
Hi Sebastian,

I'm not an expert on these parts of ECLiPSe, but I'll tell you what I can
(perhaps someone else can fill in any missing bits).

On Thu, Oct 31, 2002 at 11:05:27PM -0500, Sebastian Sardina wrote:
> I want to execute in Prolog a Unix command, say 'ls', and send its
> output to a file. I have to do it with exec_group (that is, no sh system
> or others.)
> 1) My first bet was:
> [eclipse 10]: exec_group("ls > file.txt",[],P).
> P = 7701
> Yes (0.00s cpu)
> [eclipse 11]: ls: >: No such file or directory
> ls: file.txt: No such file or directory

This does not work because stream redirection using >, <, |, etc. is a shell
feature, and as you state, you are not using (and don't want to use) a

> 2) My other "solution" was to open a file, and attach it to the output
> stream of exec_group:
> [eclipse 11]: open('file.txt',write,myfile),
> exec_group("ls",[null,myfile],P), 
> P = 7706
> Yes (0.00s cpu)
> This didn't not work either.! Even after doing a wait(P,X) and
> close(myfile), nothing is printed to the file!!

If you read the documentation for exec_group carefully, it states in part

	e.g. Stdin must be an output stream because it is connected to the
	standard input of the child process.

Thus, when you specify "myfile" in the second argument, it must be an input
stream, because it will be attached to the output stream of the command.
You've opened myfile for writing, and so it is an output stream; hence you
are attaching an output stream to an output stream, and nothing much
happens.  Note that if you subsequently try to read from myfile, you will
get the output of "ls".

I don't know if there's a way to send the output directly to the file
without sending it back into ECLiPSe first.

Unless somebody comes up with a better suggestion, one way to do what you
want would be to exec_group the ls command, then read the output in ECLiPSe
and write it out again to whereever you want it.

Received on Fri Nov 01 13:58:26 2002

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:17 PM GMT GMT