Executable Octave Programs - Untitled
Description: Untitled

Next: Comments, Previous: Errors, Up: Getting Started

### 2.6 Executable Octave Programs

Once you have learned Octave, you may want to write self-contained Octave scripts, using the ``#!`' script mechanism. You can do this on GNU systems and on many Unix systems 1.

Self-contained Octave scripts are useful when you want to write a program which users can invoke without knowing that the program is written in the Octave language.

For example, you could create a text file named `hello`, containing the following lines:

         #! octave-interpreter-name -qf
         # a sample Octave program
         printf ("Hello, world!\n");

(where octave-interpreter-name should be replaced with the full file name for your Octave binary). Note that this will only work if ``#!`' appears at the very beginning of the file. After making this file executable (with the `chmod` command), you can simply type:

         hello

at the shell, and the system will arrange to run Octave as if you had typed:

         octave hello

The line beginning with ``#!`' lists the full file name of an interpreter to be run, and an optional initial command line argument to pass to that interpreter. The operating system then runs the interpreter with the given argument and the full argument list of the executed program. The first argument in the list is the full file name of the Octave program. The rest of the argument list will either be options to Octave, or data files, or both. The ``-qf`' option is usually specified in stand-alone Octave programs to prevent them from printing the normal startup message, and to keep them from behaving differently depending on the contents of a particular user's `~/.octaverc` file. See Invoking Octave from the Command Line.

Note that some operating systems may place a limit on the number of characters that are recognized after ``#!`'. Also, the various shells/systems parse differently the arguments appearing in a ``#!`' line. The majority of them group together all the arguments in a string and pass it to the interpreter as a single argument. In this case, the following script:

         #! octave-interpreter-name -q -f # comment

is equivalent to type at the command line:

         octave "-q -f # comment"

which would obviously produce an error message. Unfortunately, it is impossible for Octave to know whether it has been called from the command line or from a ``#!`' script, so some care is needed when using the ``#!`' mechanism.

Note that when Octave is started from an executable script, the built-in function `argv` returns a cell array containing the command line arguments passed to an executable Octave script, not the arguments passed to the Octave interpreter on the ``#!`' line of the script. For example, the following program will reproduce the command line that is used to execute script, not ``-qf`'.

         #! /bin/octave -qf
         printf ("%s", program_name ());
         arg_list = argv ();
         for i = 1:nargin
           printf (" %s", arg_list{i});
         endfor
         printf ("\n");

#### Footnotes

[1] The ``#!`' mechanism works on Unix systems derived from Berkeley Unix, System V Release 4, and some System V Release 3 systems.