Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 179

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Executable Octave Programs - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Getting-Started.html#Getting-Started" title="Getting Started">
<link rel="prev" href="Errors.html#Errors" title="Errors">
<link rel="next" href="Comments.html#Comments" title="Comments">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Executable-Octave-Programs"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Comments.html#Comments">Comments</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Errors.html#Errors">Errors</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Getting-Started.html#Getting-Started">Getting Started</a>
<hr>
</div>

<h3 class="section">2.6 Executable Octave Programs</h3>

<p><a name="index-executable-scripts-153"></a><a name="index-scripts-154"></a><a name="index-batch-processing-155"></a><a name="index-self-contained-programs-156"></a><a name="index-program_002c-self-contained-157"></a><a name="index-g_t_0040samp_007b_0023_0021_007d-158"></a>
Once you have learned Octave, you may want to write self-contained
Octave scripts, using the &lsquo;<samp><span class="samp">#!</span></samp>&rsquo; script mechanism.  You can do this
on GNU systems and on many Unix systems <a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>.

   <p>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.  Octave scripts are also used for batch
processing of data files.  Once an algorithm has been developed and tested
in the interactive portion of Octave, it can be committed to an executable
script and used again and again on new data files.

   <p>As a trivial example of an executable Octave script, you might create a
text file named <samp><span class="file">hello</span></samp>, containing the following lines:

<pre class="example">     #! <var>octave-interpreter-name</var> -qf
     # a sample Octave program
     printf ("Hello, world!\n");
</pre>
   <p class="noindent">(where <var>octave-interpreter-name</var> should be replaced with the full
path and name of your Octave binary).  Note that this will only work if
&lsquo;<samp><span class="samp">#!</span></samp>&rsquo; appears at the very beginning of the file.  After making the
file executable (with the <code>chmod</code> command on Unix systems), you can
simply type:

<pre class="example">     hello
</pre>
   <p class="noindent">at the shell, and the system will arrange to run Octave as if you had
typed:

<pre class="example">     octave hello
</pre>
   <p>The line beginning with &lsquo;<samp><span class="samp">#!</span></samp>&rsquo; lists the full path and filename 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 executable.  The rest of the argument list will either be
options to Octave, or data files, or both.  The &lsquo;<samp><span class="samp">-qf</span></samp>&rsquo; options are
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
<samp><span class="file">~/.octaverc</span></samp> file.  See <a href="Invoking-Octave-from-the-Command-Line.html#Invoking-Octave-from-the-Command-Line">Invoking Octave from the Command Line</a>.

   <p>Note that some operating systems may place a limit on the number of
characters that are recognized after &lsquo;<samp><span class="samp">#!</span></samp>&rsquo;.  Also, the arguments
appearing in a &lsquo;<samp><span class="samp">#!</span></samp>&rsquo; line are parsed differently by various
shells/systems.  The majority of them group all the arguments together in one
string and pass it to the interpreter as a single argument.  In this case, the
following script:

<pre class="example">     #! <var>octave-interpreter-name</var> -q -f # comment
</pre>
   <p class="noindent">is equivalent to typing at the command line:

<pre class="example">     octave "-q -f # comment"
</pre>
   <p class="noindent">which will produce an error message.  Unfortunately, it is
not possible for Octave to determine whether it has been called from the
command line or from a &lsquo;<samp><span class="samp">#!</span></samp>&rsquo; script, so some care is needed when using the
&lsquo;<samp><span class="samp">#!</span></samp>&rsquo; mechanism.

   <p>Note that when Octave is started from an executable script, the built-in
function <code>argv</code> returns a cell array containing the command line
arguments passed to the executable Octave script, not the arguments
passed to the Octave interpreter on the &lsquo;<samp><span class="samp">#!</span></samp>&rsquo; line of the script. 
For example, the following program will reproduce the command line that
was used to execute the script, not &lsquo;<samp><span class="samp">-qf</span></samp>&rsquo;.

<pre class="example">     #! /bin/octave -qf
     printf ("%s", program_name ());
     arg_list = argv ();
     for i = 1:nargin
       printf (" %s", arg_list{i});
     endfor
     printf ("\n");
</pre>
   <div class="footnote">
<hr>
<h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> The &lsquo;<samp><span class="samp">#!</span></samp>&rsquo;
mechanism works on Unix systems derived from Berkeley Unix, System V
Release 4, and some System V Release 3 systems.</p>

   <hr></div>

   </body></html>