Unix system commands

Suppose, you're writing an executable that must be able to know which files are around and then do something usefull with that information. As an example, let's pretend we're in a diectory with lots of JPEG images and we want to make a gallery out of it. Each picture must be shown in a table entry plus some information for people to watch.
Then the things we want to know are:

When we're on the command prompt, we would issue a few simple commands like:
       ls | grep jpg | wc
       ls -l *jpg
   
and that would solve the bulk of the problem.

But how to do so from a running program? Can we get similar results when we're inside an executable?

Running system commands

MODULE tri;

FROM   SYSTEM		IMPORT	ADR;
FROM   SysLib		IMPORT	system;


PROCEDURE SystemCommand (VAR  command : ARRAY OF CHAR) : BOOLEAN;

BEGIN
   IF  system (ADR (command) ) = 0  THEN
      RETURN TRUE
   ELSE
      RETURN FALSE
   END
END SystemCommand;


BEGIN
   IF  SystemCommand ("ls -1 .. | grep ps > file.dir") = TRUE  THEN
      InOut.WriteString ("No error reported.")
   ELSE
      InOut.WriteString ("Error reported!")
   END;
   InOut.WriteLn;
   InOut.WriteBf
END tri.
   
See? That's easy. We just issue the command and put the data in a temporary file. Afterwards, we just open that file with TextIO.OpenInput and we plunder the data.

Page created 3 May 2005,