unix/1

Synopsis

unix(shell(+Command)) Spawns a command interpreter and executes Command. Note that, despite the name, unix/1 works on both UNIX and Windows.

unix(system(+Command)) Spawns a shell process and executes Command.

unix(system(+Command, -Status)) Spawns a shell process and executes Command. The exit status of the executed command is returned in Status.

unix(cd(+Path)) Changes working directory to Path.

unix(argv(-ArgList)) Returns in ArgList the list of commandline arguments as Prolog objects.

unix(args(-ArgList)) Returns in ArgList the list of commandline arguments as a list of atoms.

Arguments


Command term
atom corresponding to a command (or null)
Status integer
exit status of the command executed
Path atom
atom corresponding to a legal directory (or null)
ArgList list of term
list of arguments used to start up current session.

Description

unix(cd) changes the working directory of Prolog (and of Emacs if running under the editor interface) to your home directory. Note that the <ESC> x cd command under Emacs has the same effect as this, except that Emacs also provides filename completion.

If the return status of Command is 0, unix(system(Command)) succeeds, otherwise it fails.

unix(system(Command, Status)) returns the status of the executed command, similar to the function system(3). The low-order 8 bits of the Status is the value returned by the system call wait(2V) and the next 8-bits higher up in the Status has the shell exit status if the shell was not interrupted by a signal. An exit status of 127 indicates that the shell could not be executed.

To start up an interactive shell, type unix(shell).

If ArgList is instantiated to a term that does not unify with the result returned, unix(argv(ArgList)) or unix(args(ArgList)) will simply fail.

Exceptions

instantiation_error
Argument to unix/1 is not sufficiently instantiated.
domain_error
Argument to unix/1 is invalid.
type_error
Path is not an atom.
existence_error
Path is a nonexistent directory.

Examples

To list the QOF files in the current working directory:

     | ?- unix(shell('ls -l *.qof')).
     -rw-rw-r--  1 joe   9152 Oct 20  1990 table.qof
     -rw-rw-r--  1 joe    576 Oct 25  1990 test.qof
     
     yes
     

Alternatively, enter a command interpreter, execute commands, and type exit to return to prolog:

     | ?- unix(shell).
     % ls -l *.qof
     -rw-rw-r--  1 joe   9152 Oct 20  1990 table.qof
     -rw-rw-r--  1 joe    576 Oct 25  1990 test.qof
     % exit
     
     yes
     | ?-
     

If Prolog was invoked using the command (A), the command line arguments can be retrieved as in (B):

     % prolog screaming yellow + yellow.pl (A)
         .
         .
         .
     
     | ?- unix(argv(ArgList)). (B)
     
     ArgList = [screaming, yellow]
     

See Also

QP_initialize(), QP_toplevel(), system/1 -- from library(strings) ref-aos