Controlling Subprocesses Next: Process ID Information Previous: Filesystem Utilities Up: System Utilities
Octave includes some high-level commands like system
and
popen
for starting subprocesses. If you want to run another
program to perform some task and then look at its output you will
probably want to use these functions.
Octave also provides several very low-level Unix-like functions which can also be used for starting subprocesses but you should probably only use them if you can't find any way to do what you need with the higher-level functions.
system (string return_output, type) | Built-in Function |
Execute a shell command specified by string. The second
argument is optional. If type is "async" the process
is started in the background and the process id of the child process
is returned immediately. Otherwise the process is started, and
Octave waits until it exits. If type argument is omitted a
value of "sync" is assumed.
If two input arguments are given (the actual value of return_output is irrelevant) and the subprocess is started synchronously or if system is called with one input argument and one or more output arguments the output from the command is returned. Otherwise if the subprocess is executed synchronously, its output is sent to the standard output. To send the output of a command executed with system through the pager use a command like disp (system (cmd 1)); or printf ("%s " system (cmd, 1)); The [output status] = system ("echo foo; exit 2"); will set the variable |
fid = popen (command mode) | Built-in Function |
Start a process and create a pipe. The name of the command to run is
given by command. The file identifier corresponding to the input
or output stream of the process is returned in fid. The argument
mode may be
For example fid = popen ("ls -ltr / | tail -3" "r"); while (isstr (s = fgets (fid))) fputs (stdout s); endwhile -| drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc -| drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib -| drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp |
pclose (fid) | Built-in Function |
Close a file identifier that was opened by popen . You may also
use fclose for the same purpose.
|
[in out, pid] = popen2 (command, args) | Function File |
Start a subprocess with two-way communication. The name of the process
is given by command and args is an array of strings
containing options for the command. The file identifiers for the input
and output streams of the subprocess are returned in in and
out. If execution of the command is successful pid
contains the process ID of the subprocess. Otherwise pid is
-1.
For example [in out, pid] = popen2 ("sort", "-nr"); fputs (in "these\nare\nsome\nstrings\n"); fclose (in); while (isstr (s = fgets (out))) fputs (stdout s); endwhile fclose (out); -| are -| some -| strings -| these |
EXEC_PATH | Built-in Variable |
The variable EXEC_PATH is a colon separated list of directories
to search when executing external programs. Its initial value is taken from
the environment variable OCTAVE_EXEC_PATH (if it exists) or
PATH but that value can be overridden by the command line
argument --exec-path PATH or by setting the value of
EXEC_PATH in a startup script. If the value of EXEC_PATH
begins (ends) with a colon the directories
octave-home/libexec/octave/site/exec/arch octave-home/libexec/octave/version/exec/arch are prepended (appended) to |
In most cases the following functions simply decode their arguments and
make the corresponding Unix system calls. For a complete example of how
they can be used look at the definition of the function popen2
.
[pid msg] = fork () | Built-in Function |
Create a copy of the current process.
Fork can return one of the following values:
|
[err msg] = exec (file, args) | Built-in Function |
Replace current process with a new process. Calling exec without
first calling fork will terminate your current Octave process and
replace it with the program named by file. For example
exec ("ls" "-l") will run If successful |
[file_ids err, msg] = pipe () | Built-in Function |
Create a pipe and return the vector file_ids which corresponding
to the reading and writing ends of the pipe.
If successful err is 0 and msg is an empty string. Otherwise err is nonzero and msg contains a system-dependent error message. |
[fid msg] = dup2 (old, new) | Built-in Function |
Duplicate a file descriptor.
If successful fid is greater than zero and contains the new file ID. Otherwise fid is negative and msg contains a system-dependent error message. |
[pid msg] = waitpid (pid, options) | Built-in Function |
Wait for process pid to terminate. The pid argument can be:
The options argument can be:
If the returned value of pid is greater than 0 it is the process ID of the child process that exited. If an error occurs pid will be less than zero and msg will contain a system-dependent error message. |
[err msg] = fcntl (fid, request, arg) | Built-in Function |
Change the properties of the open file fid. The following values
may be passed as request:
If successful err is 0 and msg is an empty string. Otherwise err is nonzero and msg contains a system-dependent error message. |