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.
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
system
function can return two values. The first is any output from the command that was written to the standard output stream, and the second is the output status of the command. For example,[output, status] = system ("echo foo; exit 2");will set the variable
output
to the string `foo', and the variablestatus
to the integer `2'.
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
"r"
- The pipe will be connected to the standard output of the process, and open for reading.
"w"
- The pipe will be connected to the standard input of the process, and open for writing.
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
Close a file identifier that was opened by
popen
. You may also usefclose
for the same purpose.
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
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 variableOCTAVE_EXEC_PATH
(if it exists) orPATH
, but that value can be overridden by the command line argument--exec-path PATH
, or by setting the value ofEXEC_PATH
in a startup script. If the value ofEXEC_PATH
begins (ends) with a colon, the directoriesoctave-home/libexec/octave/site/exec/arch octave-home/libexec/octave/version/exec/archare prepended (appended) to
EXEC_PATH
, where octave-home is the top-level directory where all of Octave is installed (the default value is /usr). If you don't specify a value forEXEC_PATH
explicitly, these special directories are prepended to your shell path.
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
.
Create a copy of the current process.
Fork can return one of the following values:
- > 0
- You are in the parent process. The value returned from
fork
is the process id of the child process. You should probably arrange to wait for any child processes to exit.- 0
- You are in the child process. You can call
exec
to start another process. If that fails, you should probably callexit
.- < 0
- The call to
fork
failed for some reason. You must take evasive action. A system dependent error message will be waiting in msg.
Replace current process with a new process. Calling
exec
without first callingfork
will terminate your current Octave process and replace it with the program named by file. For example,exec ("ls" "-l")will run
ls
and return you to your shell prompt.If successful,
exec
does not return. Ifexec
does return, err will be nonzero, and msg will contain a system-dependent error message.
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.
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.
Wait for process pid to terminate. The pid argument can be:
- −1
- Wait for any child process.
- 0
- Wait for any child process whose process group ID is equal to that of the Octave interpreter process.
- > 0
- Wait for termination of the child process with ID pid.
The options argument can be:
- 0
- Wait until signal is received or a child process exits (this is the default if the options argument is missing).
- 1
- Do not hang if status is not immediately available.
- 2
- Report the status of any child processes that are stopped, and whose status has not yet been reported since they stopped.
- 3
- Implies both 1 and 2.
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.
Change the properties of the open file fid. The following values may be passed as request:
F_DUPFD
- Return a duplicate file descriptor.
F_GETFD
- Return the file descriptor flags for fid.
F_SETFD
- Set the file descriptor flags for fid.
F_GETFL
- Return the file status flags for fid. The following codes may be returned (some of the flags may be undefined on some systems).
O_RDONLY
- Open for reading only.
O_WRONLY
- Open for writing only.
O_RDWR
- Open for reading and writing.
O_APPEND
- Append on each write.
O_CREAT
- Create the file if it does not exist.
O_NONBLOCK
- Nonblocking mode.
O_SYNC
- Wait for writes to complete.
O_ASYNC
- Asynchronous I/O.
F_SETFL
- Set the file status flags for fid to the value specified by arg. The only flags that can be changed are
O_APPEND
andO_NONBLOCK
.If successful, err is 0 and msg is an empty string. Otherwise, err is nonzero and msg contains a system-dependent error message.