Go to the previous, next section.

System Utilities

This chapter describes the functions that are available to allow you to get information about what is happening outside of Octave, while it is still running, and use this information in your program. For example, you can get information about environment variables, the current time, and even start other programs from the Octave prompt.

Timing Utilities

The function clock returns a vector containing the current year, month (1-12), day (1-31), hour (0-23), minute (0-59) and second (0-60). For example,

octave:13> clock
ans =

  1993     8    20     4    56     1

The function clock is more accurate on systems that have the gettimeofday function.

To get the date as a character string in the form DD-MMM-YY, use the command date. For example,

octave:13> date
ans = 20-Aug-93

Octave also has functions for computing time intervals and CPU time used. The functions tic and toc can be used to set and check a wall-clock timer. For example,

tic ();
# many computations later...
elapsed_time = toc ();

will set the variable elapsed_time to the number of seconds since the most recent call to the function tic.

The function etime provides another way to get elapsed wall-clock time by returning the difference (in seconds) between two time values returned from clock. For example:

t0 = clock ();
# many computations later...
elapsed_time = etime (clock (), t0);

will set the variable elapsed_time to the number of seconds since the variable t0 was set.

The function cputime allows you to obtain information about the amount of CPU time your Octave session is using. For example,

[total, user, system] = cputime ();

returns the CPU time used by your Octave session. The first output is the total time spent executing your process and is equal to the sum of second and third outputs, which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode, respectively.

Finally, Octave's function is_leap_year returns 1 if the given year is a leap year and 0 otherwise. If no arguments are provided, is_leap_year will use the current year. For example,

octave:13> is_leap_year (2000)
ans = 1

Contrary to what many people who post misinformation to Usenet apparently believe, Octave knows that the year 2000 will be a leap year.

Interacting with the OS

You can execute any shell command using the function system (cmd, flag). The second argument is optional. If it is present, the output of the command is returned by system as a string. If it is not supplied, any output from the command is printed, with the standard output filtered through the pager. For example,

users = system ("finger", 1)

places the output of the command finger in the variable users.

If you want to execute a shell command and have it behave as if it were typed directly from the shell prompt, you may need to specify extra arguments for the command. For example, to get bash to behave as an interactive shell, you can type

system ("bash -i >/dev/tty");

The first argument, `-i', tells bash to behave as an interactive shell, and the redirection of the standard output stream prevents any output produced by bash from being sent back to Octave, where it would be buffered until Octave displays another prompt.

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 variable status to the integer `2'.

The name shell_cmd exists for compatibility with earlier versions of Octave.

You can find the values of environment variables using the function getenv. For example,

getenv ("PATH")

returns a string containing the value of your path.

The functions clc, and home clear your terminal screen and move the cursor to the upper left corner.

You can change the current working directory using the cd command. Tilde expansion is performed on the path. For example,

cd ~/octave

Changes the current working directory to `~/octave'. If the directory does not exist, an error message is printed and the working directory is not changed.

The name chdir is an alias for cd.

The command pwd prints the current working directory.

The functions dir and ls list directory contents. For example,

octave:13> ls -l
total 12
-rw-r--r--   1 jwe      users        4488 Aug 19 04:02 foo.m
-rw-r--r--   1 jwe      users        1315 Aug 17 23:14 bar.m

The dir and ls commands are implemented by calling your system's directory listing command, so the available options may vary from system to system.

System Information

If possible, computer prints a string of the form cpu-vendor-os that identifies the kind of computer Octave is running on. For example,

octave:13> computer
sparc-sun-sunos4.1.2

The function isieee returns 1 if your computer claims to conform to the IEEE standard for floating point calculations.

The function version returns Octave's version number as a string. This is also the value of the built-in variable OCTAVE_VERSION. See section Built-in Variables.

Other Functions

The function pause allows you to suspend the execution of a program. If invoked without any arguments, Octave waits until you type a character. With a numeric argument, it pauses for the given number of seconds. For example, the following statement prints a message and then waits 5 seconds before clearing the screen.

fprintf (stderr, "wait please...\n"), pause (5), clc

Go to the previous, next section.