3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Getting Started |
3294
|
6 @chapter Getting Started |
|
7 |
|
8 This chapter explains some of Octave's basic features, including how to |
|
9 start an Octave session, get help at the command prompt, edit the |
|
10 command line, and write Octave programs that can be executed as commands |
|
11 from your shell. |
|
12 |
|
13 @menu |
|
14 * Invoking Octave:: |
|
15 * Quitting Octave:: |
|
16 * Getting Help:: |
|
17 * Command Line Editing:: |
|
18 * Errors:: |
|
19 * Executable Octave Programs:: |
|
20 * Comments:: |
|
21 @end menu |
|
22 |
4167
|
23 @node Invoking Octave |
3294
|
24 @section Invoking Octave |
|
25 |
|
26 Normally, Octave is used interactively by running the program |
|
27 @samp{octave} without any arguments. Once started, Octave reads |
|
28 commands from the terminal until you tell it to exit. |
|
29 |
|
30 You can also specify the name of a file on the command line, and Octave |
|
31 will read and execute the commands from the named file and then exit |
|
32 when it is finished. |
|
33 |
|
34 You can further control how Octave starts by using the command-line |
|
35 options described in the next section, and Octave itself can remind you |
|
36 of the options available. Type @samp{octave --help} to display all |
|
37 available options and briefly describe their use (@samp{octave -h} is a |
|
38 shorter equivalent). |
|
39 |
|
40 @menu |
|
41 * Command Line Options:: |
|
42 * Startup Files:: |
|
43 @end menu |
|
44 |
4167
|
45 @node Command Line Options |
3294
|
46 @subsection Command Line Options |
|
47 @cindex Octave command options |
|
48 @cindex command options |
|
49 @cindex options, Octave command |
|
50 |
|
51 Here is a complete list of all the command line options that Octave |
|
52 accepts. |
|
53 |
|
54 @table @code |
|
55 @item --debug |
|
56 @itemx -d |
|
57 @cindex @code{--debug} |
|
58 @cindex @code{-d} |
|
59 Enter parser debugging mode. Using this option will cause Octave's |
|
60 parser to print a lot of information about the commands it reads, and is |
|
61 probably only useful if you are actually trying to debug the parser. |
|
62 |
|
63 @item --echo-commands |
|
64 @itemx -x |
|
65 @cindex @code{--echo-commands} |
|
66 @cindex @code{-x} |
|
67 Echo commands as they are executed. |
|
68 |
5190
|
69 @item --eval @var{code} |
|
70 Evaluate @var{code} and exit when done unless @code{--persist} is also |
|
71 specified. |
|
72 |
3294
|
73 @item --exec-path @var{path} |
|
74 @cindex @code{--exec-path @var{path}} |
|
75 Specify the path to search for programs to run. The value of @var{path} |
|
76 specified on the command line will override any value of |
|
77 @code{OCTAVE_EXEC_PATH} found in the environment, but not any commands |
|
78 in the system or user startup files that set the built-in variable |
|
79 @code{EXEC_PATH}. |
|
80 |
|
81 @item --help |
|
82 @itemx -h |
|
83 @itemx -? |
|
84 @cindex @code{--help} |
|
85 @cindex @code{-h} |
|
86 @cindex @code{-?} |
|
87 Print short help message and exit. |
|
88 |
|
89 @item --info-file @var{filename} |
|
90 @cindex @code{--info-file @var{filename}} |
|
91 Specify the name of the info file to use. The value of @var{filename} |
|
92 specified on the command line will override any value of |
|
93 @code{OCTAVE_INFO_FILE} found in the environment, but not any commands |
6477
|
94 in the system or user startup files that use the the @code{info_file} |
|
95 function. |
3294
|
96 |
|
97 @item --info-program @var{program} |
|
98 @cindex @code{--info-program @var{program}} |
|
99 Specify the name of the info program to use. The value of @var{program} |
|
100 specified on the command line will override any value of |
|
101 @code{OCTAVE_INFO_PROGRAM} found in the environment, but not any |
6477
|
102 commands in the system or user startup files that use the |
|
103 @code{info_program} function. |
3294
|
104 |
|
105 @item --interactive |
|
106 @itemx -i |
|
107 @cindex @code{--interactive} |
|
108 @cindex @code{-i} |
|
109 Force interactive behavior. This can be useful for running Octave via a |
|
110 remote shell command or inside an Emacs shell buffer. For another way |
|
111 to run Octave within Emacs, see @ref{Emacs}. |
|
112 |
3428
|
113 @item --no-history |
|
114 @itemx -H |
|
115 Disable command-line history. |
|
116 |
3294
|
117 @item --no-init-file |
|
118 @cindex @code{--no-init-file} |
|
119 Don't read the @file{~/.octaverc} or @file{.octaverc} files. |
|
120 |
|
121 @item --no-line-editing |
|
122 @cindex @code{--no-line-editing} |
|
123 Disable command-line editing. |
|
124 |
|
125 @item --no-site-file |
|
126 @cindex @code{--no-site-file} |
|
127 Don't read the site-wide @file{octaverc} file. |
|
128 |
|
129 @item --norc |
|
130 @itemx -f |
|
131 @cindex @code{--norc} |
|
132 @cindex @code{-f} |
|
133 Don't read any of the system or user initialization files at startup. |
|
134 This is equivalent to using both of the options @code{--no-init-file} |
|
135 and @code{--no-site-file}. |
|
136 |
|
137 @item --path @var{path} |
|
138 @itemx -p @var{path} |
|
139 @cindex @code{--path @var{path}} |
|
140 @cindex @code{-p @var{path}} |
|
141 Specify the path to search for function files. The value of @var{path} |
|
142 specified on the command line will override any value of |
|
143 @code{OCTAVE_PATH} found in the environment, but not any commands in the |
6477
|
144 system or user startup files that set the internal variable @code{LOADPATH} |
|
145 through one of the path functions. |
3294
|
146 |
5190
|
147 @item --persist |
|
148 Go to interactive mode after @code{--eval} or reading from a file |
|
149 named on the command line. |
|
150 |
3294
|
151 @item --silent |
|
152 @itemx --quiet |
|
153 @itemx -q |
|
154 @cindex @code{--silent} |
|
155 @cindex @code{--quiet} |
|
156 @cindex @code{-q} |
|
157 Don't print the usual greeting and version message at startup. |
|
158 |
|
159 @item --traditional |
|
160 @itemx --braindead |
|
161 @cindex @code{--traditional} |
|
162 @cindex @code{--braindead} |
6556
|
163 For compatibility with @sc{Matlab}, set initial values for |
|
164 user-preferences to the following values |
3294
|
165 |
|
166 @example |
4455
|
167 @group |
6556
|
168 PS1 = ">> " |
|
169 PS2 = "" |
|
170 beep_on_error = true |
|
171 crash_dumps_octave_core = false |
|
172 default_save_options = "-mat-binary" |
|
173 fixed_point_format = true |
|
174 history_timestamp_format_string = "%%-- %D %I:%M %p --%%" |
|
175 page_screen_output = false |
|
176 print_empty_dimensions = false |
|
177 @end group |
|
178 @end example |
|
179 |
|
180 @noindent |
|
181 and disable the following warnings |
|
182 @example |
|
183 @group |
|
184 Octave:fopen-file-in-path |
|
185 Octave:function-name-clash |
|
186 Octave:load-file-in-path |
4455
|
187 @end group |
3294
|
188 @end example |
|
189 |
|
190 @item --verbose |
|
191 @itemx -V |
|
192 @cindex @code{--verbose} |
|
193 @cindex @code{-V} |
|
194 Turn on verbose output. |
|
195 |
|
196 @item --version |
|
197 @itemx -v |
|
198 @cindex @code{--version} |
|
199 @cindex @code{-v} |
|
200 Print the program version number and exit. |
|
201 |
|
202 @item @var{file} |
5190
|
203 Execute commands from @var{file}. Exit when done unless |
|
204 @code{--persist} is also specified. |
3294
|
205 @end table |
|
206 |
|
207 Octave also includes several built-in variables that contain information |
|
208 about the command line, including the number of arguments and all of the |
|
209 options. |
|
210 |
3332
|
211 @DOCSTRING(argv) |
3294
|
212 |
6550
|
213 @DOCSTRING(program_name) |
|
214 |
3332
|
215 @DOCSTRING(program_invocation_name) |
3294
|
216 |
6479
|
217 Here is an example of using these functions to reproduce Octave's |
3294
|
218 command line. |
|
219 |
|
220 @example |
6479
|
221 printf ("%s", program_name ()); |
3294
|
222 for i = 1:nargin |
6479
|
223 arg_list = argv (); |
|
224 printf (" %s", arg_list@{i@}); |
3294
|
225 endfor |
|
226 printf ("\n"); |
|
227 @end example |
|
228 |
|
229 @noindent |
3402
|
230 @xref{Index Expressions}, for an explanation of how to properly index |
|
231 arrays of strings and substrings in Octave, and @xref{Defining Functions}, |
3294
|
232 for information about the variable @code{nargin}. |
|
233 |
4167
|
234 @node Startup Files |
3294
|
235 @subsection Startup Files |
|
236 @cindex initialization |
|
237 @cindex startup |
|
238 |
|
239 When Octave starts, it looks for commands to execute from the following |
|
240 files: |
|
241 |
|
242 @cindex startup files |
|
243 |
|
244 @table @code |
|
245 @item @var{octave-home}/share/octave/site/m/startup/octaverc |
|
246 Where @var{octave-home} is the directory in which all of Octave is |
|
247 installed (the default is @file{@value{OCTAVEHOME}}). This file is |
|
248 provided so that changes to the default Octave environment can be made |
|
249 globally for all users at your site for all versions of Octave you have |
|
250 installed. Some care should be taken when making changes to this file, |
|
251 since all users of Octave at your site will be affected. |
|
252 |
|
253 @item @var{octave-home}/share/octave/@var{version}/m/startup/octaverc |
|
254 Where @var{octave-home} is the directory in which all of Octave is |
|
255 installed (the default is @file{@value{OCTAVEHOME}}), and @var{version} |
|
256 is the version number of Octave. This file is provided so that changes |
|
257 to the default Octave environment can be made globally for all users for |
|
258 a particular version of Octave. Some care should be taken when making |
|
259 changes to this file, since all users of Octave at your site will be |
|
260 affected. |
|
261 |
|
262 @item ~/.octaverc |
|
263 @cindex @code{~/.octaverc} |
|
264 This file is normally used to make personal changes to the default |
|
265 Octave environment. |
|
266 |
|
267 @item .octaverc |
|
268 @cindex @code{.octaverc} |
|
269 This file can be used to make changes to the default Octave environment |
|
270 for a particular project. Octave searches for this file in the current |
|
271 directory after it reads @file{~/.octaverc}. Any use of the @code{cd} |
|
272 command in the @file{~/.octaverc} file will affect the directory that |
|
273 Octave searches for the file @file{.octaverc}. |
|
274 |
|
275 If you start Octave in your home directory, commands from from the file |
|
276 @file{~/.octaverc} will only be executed once. |
|
277 @end table |
|
278 |
|
279 A message will be displayed as each of the startup files is read if you |
|
280 invoke Octave with the @code{--verbose} option but without the |
|
281 @code{--silent} option. |
|
282 |
|
283 Startup files may contain any valid Octave commands, including function |
|
284 definitions. |
|
285 |
6551
|
286 @DOCSTRING(OCTAVE_HOME) |
|
287 |
6550
|
288 @DOCSTRING(version) |
|
289 |
|
290 @DOCSTRING(ver) |
|
291 |
4167
|
292 @node Quitting Octave |
3294
|
293 @section Quitting Octave |
|
294 @cindex exiting octave |
|
295 @cindex quitting octave |
|
296 |
3332
|
297 @DOCSTRING(quit) |
3294
|
298 |
3332
|
299 @DOCSTRING(atexit) |
3294
|
300 |
4167
|
301 @node Getting Help |
3294
|
302 @section Commands for Getting Help |
|
303 @cindex on-line help |
|
304 @cindex help, on-line |
|
305 |
|
306 The entire text of this manual is available from the Octave prompt |
|
307 via the command @kbd{help -i}. In addition, the documentation for |
|
308 individual user-written functions and variables is also available via |
|
309 the @kbd{help} command. This section describes the commands used for |
|
310 reading the manual and the documentation strings for user-supplied |
|
311 functions and variables. @xref{Function Files}, for more information |
|
312 about how to document the functions you write. |
|
313 |
3332
|
314 @DOCSTRING(help) |
3294
|
315 |
6549
|
316 @DOCSTRING(doc) |
|
317 |
|
318 @DOCSTRING(lookfor) |
|
319 |
3294
|
320 The help command can give you information about operators, but not the |
|
321 comma and semicolons that are used as command separators. To get help |
|
322 for those, you must type @kbd{help comma} or @kbd{help semicolon}. |
|
323 |
6477
|
324 @DOCSTRING(info_file) |
3294
|
325 |
6477
|
326 @DOCSTRING(info_program) |
3294
|
327 |
6477
|
328 @DOCSTRING(makeinfo_program) |
3686
|
329 |
3332
|
330 @DOCSTRING(suppress_verbose_help_message) |
3294
|
331 |
4167
|
332 @node Command Line Editing |
3294
|
333 @section Command Line Editing |
|
334 @cindex command-line editing |
|
335 @cindex editing the command line |
|
336 |
|
337 Octave uses the GNU readline library to provide an extensive set of |
|
338 command-line editing and history features. Only the most common |
|
339 features are described in this manual. Please see The GNU Readline |
|
340 Library manual for more information. |
|
341 |
|
342 To insert printing characters (letters, digits, symbols, etc.), simply |
|
343 type the character. Octave will insert the character at the cursor and |
|
344 advance the cursor forward. |
|
345 |
|
346 Many of the command-line editing functions operate using control |
|
347 characters. For example, the character @kbd{Control-a} moves the cursor |
|
348 to the beginning of the line. To type @kbd{C-a}, hold down @key{CTRL} |
|
349 and then press @key{a}. In the following sections, control characters |
|
350 such as @kbd{Control-a} are written as @kbd{C-a}. |
|
351 |
|
352 Another set of command-line editing functions use Meta characters. On |
|
353 some terminals, you type @kbd{M-u} by holding down @key{META} and |
|
354 pressing @key{u}. If your terminal does not have a @key{META} key, you |
|
355 can still type Meta charcters using two-character sequences starting |
|
356 with @kbd{ESC}. Thus, to enter @kbd{M-u}, you could type |
|
357 @key{ESC}@key{u}. The @kbd{ESC} character sequences are also allowed on |
|
358 terminals with real Meta keys. In the following sections, Meta |
|
359 characters such as @kbd{Meta-u} are written as @kbd{M-u}. |
|
360 |
3428
|
361 |
3294
|
362 @menu |
|
363 * Cursor Motion:: |
|
364 * Killing and Yanking:: |
|
365 * Commands For Text:: |
|
366 * Commands For Completion:: |
|
367 * Commands For History:: |
3439
|
368 * Customizing readline:: |
3294
|
369 * Customizing the Prompt:: |
|
370 * Diary and Echo Commands:: |
|
371 @end menu |
|
372 |
4167
|
373 @node Cursor Motion |
3294
|
374 @subsection Cursor Motion |
|
375 |
|
376 The following commands allow you to position the cursor. |
|
377 |
|
378 @table @kbd |
|
379 @item C-b |
|
380 Move back one character. |
|
381 |
|
382 @item C-f |
|
383 Move forward one character. |
|
384 |
|
385 @item @key{DEL} |
|
386 Delete the character to the left of the cursor. |
|
387 |
|
388 @item C-d |
|
389 Delete the character underneath the cursor. |
|
390 |
|
391 @item M-f |
|
392 Move forward a word. |
|
393 |
|
394 @item M-b |
|
395 Move backward a word. |
|
396 |
|
397 @item C-a |
|
398 Move to the start of the line. |
|
399 |
|
400 @item C-e |
|
401 Move to the end of the line. |
|
402 |
|
403 @item C-l |
|
404 Clear the screen, reprinting the current line at the top. |
|
405 |
|
406 @item C-_ |
|
407 @itemx C-/ |
|
408 Undo the last thing that you did. You can undo all the way back to an |
|
409 empty line. |
|
410 |
|
411 @item M-r |
|
412 Undo all changes made to this line. This is like typing the `undo' |
|
413 command enough times to get back to the beginning. |
|
414 @end table |
|
415 |
|
416 The above table describes the most basic possible keystrokes that you need |
|
417 in order to do editing of the input line. On most terminals, you can |
|
418 also use the arrow keys in place of @kbd{C-f} and @kbd{C-b} to move |
|
419 forward and backward. |
|
420 |
|
421 Notice how @kbd{C-f} moves forward a character, while @kbd{M-f} moves |
|
422 forward a word. It is a loose convention that control keystrokes |
|
423 operate on characters while meta keystrokes operate on words. |
|
424 |
|
425 There is also a function available so that you can clear the screen from |
|
426 within Octave programs. |
|
427 |
|
428 @cindex clearing the screen |
|
429 |
3332
|
430 @DOCSTRING(clc) |
3294
|
431 |
4167
|
432 @node Killing and Yanking |
3294
|
433 @subsection Killing and Yanking |
|
434 |
|
435 @dfn{Killing} text means to delete the text from the line, but to save |
|
436 it away for later use, usually by @dfn{yanking} it back into the line. |
|
437 If the description for a command says that it `kills' text, then you can |
|
438 be sure that you can get the text back in a different (or the same) |
|
439 place later. |
|
440 |
|
441 Here is the list of commands for killing text. |
|
442 |
|
443 @table @kbd |
|
444 @item C-k |
|
445 Kill the text from the current cursor position to the end of the line. |
|
446 |
|
447 @item M-d |
|
448 Kill from the cursor to the end of the current word, or if between |
|
449 words, to the end of the next word. |
|
450 |
|
451 @item M-@key{DEL} |
|
452 Kill from the cursor to the start of the previous word, or if between |
|
453 words, to the start of the previous word. |
|
454 |
|
455 @item C-w |
|
456 Kill from the cursor to the previous whitespace. This is different than |
|
457 @kbd{M-@key{DEL}} because the word boundaries differ. |
|
458 @end table |
|
459 |
|
460 And, here is how to @dfn{yank} the text back into the line. Yanking |
|
461 means to copy the most-recently-killed text from the kill buffer. |
|
462 |
|
463 @table @kbd |
|
464 @item C-y |
|
465 Yank the most recently killed text back into the buffer at the cursor. |
|
466 |
|
467 @item M-y |
|
468 Rotate the kill-ring, and yank the new top. You can only do this if |
|
469 the prior command is @kbd{C-y} or @kbd{M-y}. |
|
470 @end table |
|
471 |
|
472 When you use a kill command, the text is saved in a @dfn{kill-ring}. |
|
473 Any number of consecutive kills save all of the killed text together, so |
|
474 that when you yank it back, you get it in one clean sweep. The kill |
|
475 ring is not line specific; the text that you killed on a previously |
|
476 typed line is available to be yanked back later, when you are typing |
|
477 another line. |
|
478 |
4167
|
479 @node Commands For Text |
3294
|
480 @subsection Commands For Changing Text |
|
481 |
|
482 The following commands can be used for entering characters that would |
|
483 otherwise have a special meaning (e.g., @kbd{TAB}, @kbd{C-q}, etc.), or |
|
484 for quickly correcting typing mistakes. |
|
485 |
|
486 @table @kbd |
|
487 @item C-q |
|
488 @itemx C-v |
|
489 Add the next character that you type to the line verbatim. This is |
|
490 how to insert things like @kbd{C-q} for example. |
|
491 |
|
492 @item M-@key{TAB} |
|
493 Insert a tab character. |
|
494 |
|
495 @item C-t |
|
496 Drag the character before the cursor forward over the character at the |
|
497 cursor, also moving the cursor forward. If the cursor is at the end of |
|
498 the line, then transpose the two characters before it. |
|
499 |
|
500 @item M-t |
|
501 Drag the word behind the cursor past the word in front of the cursor |
|
502 moving the cursor over that word as well. |
|
503 |
|
504 @item M-u |
|
505 Uppercase the characters following the cursor to the end of the current |
|
506 (or following) word, moving the cursor to the end of the word. |
|
507 |
|
508 @item M-l |
|
509 Lowecase the characters following the cursor to the end of the current |
|
510 (or following) word, moving the cursor to the end of the word. |
|
511 |
|
512 @item M-c |
|
513 Uppercase the character following the cursor (or the beginning of the |
|
514 next word if the cursor is between words), moving the cursor to the end |
|
515 of the word. |
|
516 @end table |
|
517 |
4167
|
518 @node Commands For Completion |
3294
|
519 @subsection Letting Readline Type For You |
|
520 @cindex command completion |
|
521 |
|
522 The following commands allow Octave to complete command and variable |
|
523 names for you. |
|
524 |
|
525 @table @kbd |
|
526 @item @key{TAB} |
|
527 Attempt to do completion on the text before the cursor. Octave can |
|
528 complete the names of commands and variables. |
|
529 |
|
530 @item M-? |
|
531 List the possible completions of the text before the cursor. |
|
532 @end table |
|
533 |
3332
|
534 @DOCSTRING(completion_append_char) |
3294
|
535 |
3332
|
536 @DOCSTRING(completion_matches) |
3294
|
537 |
4167
|
538 @node Commands For History |
3294
|
539 @subsection Commands For Manipulating The History |
|
540 @cindex command history |
|
541 @cindex input history |
|
542 @cindex history of commands |
|
543 |
|
544 Octave normally keeps track of the commands you type so that you can |
|
545 recall previous commands to edit or execute them again. When you exit |
|
546 Octave, the most recent commands you have typed, up to the number |
|
547 specified by the variable @code{history_size}, are saved in a file. |
|
548 When Octave starts, it loads an initial list of commands from the file |
|
549 named by the variable @code{history_file}. |
|
550 |
|
551 Here are the commands for simple browsing and searching the history |
|
552 list. |
|
553 |
|
554 @table @kbd |
|
555 @item @key{LFD} |
|
556 @itemx @key{RET} |
|
557 Accept the line regardless of where the cursor is. If this line is |
|
558 non-empty, add it to the history list. If this line was a history |
|
559 line, then restore the history line to its original state. |
|
560 |
|
561 @item C-p |
|
562 Move `up' through the history list. |
|
563 |
|
564 @item C-n |
|
565 Move `down' through the history list. |
|
566 |
|
567 @item M-< |
|
568 Move to the first line in the history. |
|
569 |
|
570 @item M-> |
|
571 Move to the end of the input history, i.e., the line you are entering! |
|
572 |
|
573 @item C-r |
|
574 Search backward starting at the current line and moving `up' through |
|
575 the history as necessary. This is an incremental search. |
|
576 |
|
577 @item C-s |
|
578 Search forward starting at the current line and moving `down' through |
|
579 the history as necessary. |
|
580 @end table |
|
581 |
|
582 On most terminals, you can also use the arrow keys in place of @kbd{C-p} |
|
583 and @kbd{C-n} to move through the history list. |
|
584 |
|
585 In addition to the keyboard commands for moving through the history |
|
586 list, Octave provides three functions for viewing, editing, and |
|
587 re-running chunks of commands from the history list. |
|
588 |
3332
|
589 @DOCSTRING(history) |
3294
|
590 |
3332
|
591 @DOCSTRING(edit_history) |
3294
|
592 |
3332
|
593 @DOCSTRING(run_history) |
3294
|
594 |
3368
|
595 @DOCSTRING(EDITOR) |
3294
|
596 |
3332
|
597 @DOCSTRING(history_file) |
3294
|
598 |
3332
|
599 @DOCSTRING(history_size) |
3294
|
600 |
3332
|
601 @DOCSTRING(saving_history) |
3294
|
602 |
6550
|
603 @DOCSTRING(history_timestamp_format_string) |
|
604 |
4167
|
605 @node Customizing readline |
3428
|
606 @subsection Customizing @code{readline} |
|
607 |
5775
|
608 @c FIXME -- need a brief description of the ~/.inputrc file here. |
3428
|
609 |
3439
|
610 @DOCSTRING(read_readline_init_file) |
3428
|
611 |
4167
|
612 @node Customizing the Prompt |
3294
|
613 @subsection Customizing the Prompt |
|
614 @cindex prompt customization |
|
615 @cindex customizing the prompt |
|
616 |
|
617 The following variables are available for customizing the appearance of |
|
618 the command-line prompts. Octave allows the prompt to be customized by |
|
619 inserting a number of backslash-escaped special characters that are |
|
620 decoded as follows: |
|
621 |
|
622 @table @samp |
|
623 @item \t |
|
624 The time. |
|
625 |
|
626 @item \d |
|
627 The date. |
|
628 |
|
629 @item \n |
|
630 Begins a new line by printing the equivalent of a carriage return |
|
631 followed by a line feed. |
|
632 |
|
633 @item \s |
|
634 The name of the program (usually just @samp{octave}). |
|
635 |
|
636 @item \w |
|
637 The current working directory. |
|
638 |
|
639 @item \W |
|
640 The basename of the current working directory. |
|
641 |
|
642 @item \u |
|
643 The username of the current user. |
|
644 |
|
645 @item \h |
|
646 The hostname, up to the first `.'. |
|
647 |
|
648 @item \H |
|
649 The hostname. |
|
650 |
|
651 @item \# |
|
652 The command number of this command, counting from when Octave starts. |
|
653 |
|
654 @item \! |
|
655 The history number of this command. This differs from @samp{\#} by the |
|
656 number of commands in the history list when Octave starts. |
|
657 |
|
658 @item \$ |
|
659 If the effective UID is 0, a @samp{#}, otherwise a @samp{$}. |
|
660 |
|
661 @item \nnn |
|
662 The character whose character code in octal is @var{nnn}. |
|
663 |
|
664 @item \\ |
|
665 A backslash. |
|
666 @end table |
|
667 |
3332
|
668 @DOCSTRING(PS1) |
3294
|
669 |
3332
|
670 @DOCSTRING(PS2) |
3294
|
671 |
3332
|
672 @DOCSTRING(PS4) |
3294
|
673 |
4167
|
674 @node Diary and Echo Commands |
3294
|
675 @subsection Diary and Echo Commands |
|
676 @cindex diary of commands and output |
|
677 @cindex command and ouput logs |
|
678 @cindex logging commands and output |
|
679 @cindex echoing executing commands |
|
680 @cindex command echoing |
|
681 |
|
682 Octave's diary feature allows you to keep a log of all or part of an |
|
683 interactive session by recording the input you type and the output that |
|
684 Octave produces in a separate file. |
|
685 |
3332
|
686 @DOCSTRING(diary) |
3294
|
687 |
|
688 Sometimes it is useful to see the commands in a function or script as |
|
689 they are being evaluated. This can be especially helpful for debugging |
|
690 some kinds of problems. |
|
691 |
3332
|
692 @DOCSTRING(echo) |
3294
|
693 |
3332
|
694 @DOCSTRING(echo_executing_commands) |
3294
|
695 |
4167
|
696 @node Errors |
3294
|
697 @section How Octave Reports Errors |
|
698 @cindex error messages |
|
699 @cindex messages, error |
|
700 |
|
701 Octave reports two kinds of errors for invalid programs. |
|
702 |
|
703 A @dfn{parse error} occurs if Octave cannot understand something you |
|
704 have typed. For example, if you misspell a keyword, |
|
705 |
|
706 @example |
|
707 octave:13> functon y = f (x) y = x^2; endfunction |
|
708 @end example |
|
709 |
|
710 @noindent |
|
711 Octave will respond immediately with a message like this: |
|
712 |
|
713 @example |
|
714 parse error: |
|
715 |
|
716 functon y = f (x) y = x^2; endfunction |
|
717 ^ |
|
718 @end example |
|
719 |
|
720 @noindent |
|
721 For most parse errors, Octave uses a caret (@samp{^}) to mark the point |
|
722 on the line where it was unable to make sense of your input. In this |
|
723 case, Octave generated an error message because the keyword |
|
724 @code{function} was misspelled. Instead of seeing @samp{function f}, |
|
725 Octave saw two consecutive variable names, which is invalid in this |
|
726 context. It marked the error at @code{y} because the first name by |
|
727 itself was accepted as valid input. |
|
728 |
|
729 Another class of error message occurs at evaluation time. These |
|
730 errors are called @dfn{run-time errors}, or sometimes |
|
731 @dfn{evaluation errors} because they occur when your program is being |
|
732 @dfn{run}, or @dfn{evaluated}. For example, if after correcting the |
|
733 mistake in the previous function definition, you type |
|
734 |
|
735 @example |
|
736 octave:13> f () |
|
737 @end example |
|
738 |
|
739 @noindent |
|
740 Octave will respond with |
|
741 |
|
742 @example |
|
743 @group |
|
744 error: `x' undefined near line 1 column 24 |
|
745 error: evaluating expression near line 1, column 24 |
|
746 error: evaluating assignment expression near line 1, column 22 |
|
747 error: called from `f' |
|
748 @end group |
|
749 @end example |
|
750 |
|
751 This error message has several parts, and gives you quite a bit of |
|
752 information to help you locate the source of the error. The messages |
|
753 are generated from the point of the innermost error, and provide a |
|
754 traceback of enclosing expressions and function calls. |
|
755 |
|
756 In the example above, the first line indicates that a variable named |
|
757 @samp{x} was found to be undefined near line 1 and column 24 of some |
|
758 function or expression. For errors occurring within functions, lines |
|
759 are counted from the beginning of the file containing the function |
|
760 definition. For errors occurring at the top level, the line number |
|
761 indicates the input line number, which is usually displayed in the |
|
762 prompt string. |
|
763 |
|
764 The second and third lines in the example indicate that the error |
|
765 occurred within an assignment expression, and the last line of the error |
|
766 message indicates that the error occurred within the function @code{f}. |
|
767 If the function @code{f} had been called from another function, for |
|
768 example, @code{g}, the list of errors would have ended with one more |
|
769 line: |
|
770 |
|
771 @example |
|
772 error: called from `g' |
|
773 @end example |
|
774 |
|
775 These lists of function calls usually make it fairly easy to trace the |
|
776 path your program took before the error occurred, and to correct the |
|
777 error before trying again. |
|
778 |
4167
|
779 @node Executable Octave Programs |
3294
|
780 @section Executable Octave Programs |
|
781 @cindex executable scripts |
|
782 @cindex scripts |
|
783 @cindex self contained programs |
|
784 @cindex program, self contained |
|
785 @cindex @samp{#!} |
|
786 |
|
787 Once you have learned Octave, you may want to write self-contained |
|
788 Octave scripts, using the @samp{#!} script mechanism. You can do this |
|
789 on GNU systems and on many Unix systems @footnote{The @samp{#!} |
|
790 mechanism works on Unix systems derived from Berkeley Unix, System V |
6481
|
791 Release 4, and some System V Release 3 systems.}. |
|
792 |
|
793 Self-contained Octave scripts are useful when you want to write a |
|
794 program which users can invoke without knowing that the program is |
|
795 written in the Octave language. |
3294
|
796 |
|
797 For example, you could create a text file named @file{hello}, containing |
|
798 the following lines: |
|
799 |
|
800 @example |
|
801 @group |
|
802 #! @var{octave-interpreter-name} -qf |
|
803 # a sample Octave program |
|
804 printf ("Hello, world!\n"); |
|
805 @end group |
|
806 @end example |
|
807 |
|
808 @noindent |
|
809 (where @var{octave-interpreter-name} should be replaced with the full |
6481
|
810 file name for your Octave binary). Note that this will only work if |
|
811 @samp{#!} appears at the very beginning of the file. After making this |
|
812 file executable |
3294
|
813 (with the @code{chmod} command), you can simply type: |
|
814 |
|
815 @example |
|
816 hello |
|
817 @end example |
|
818 |
|
819 @noindent |
|
820 at the shell, and the system will arrange to run Octave as if you had |
|
821 typed: |
|
822 |
|
823 @example |
|
824 octave hello |
|
825 @end example |
|
826 |
|
827 The line beginning with @samp{#!} lists the full file name of an |
|
828 interpreter to be run, and an optional initial command line argument to |
|
829 pass to that interpreter. The operating system then runs the |
|
830 interpreter with the given argument and the full argument list of the |
|
831 executed program. The first argument in the list is the full file name |
|
832 of the Octave program. The rest of the argument list will either be |
|
833 options to Octave, or data files, or both. The @samp{-qf} option is |
|
834 usually specified in stand-alone Octave programs to prevent them from |
|
835 printing the normal startup message, and to keep them from behaving |
|
836 differently depending on the contents of a particular user's |
6481
|
837 @file{~/.octaverc} file. @xref{Invoking Octave}. |
|
838 |
|
839 Note that some operating systems may place a limit on the number of |
|
840 characters that are recognized after @samp{#!}. Also, the various |
|
841 shells/systems parse differently the arguments appearing in a @samp{#!} |
|
842 line. The majority of them group together all the arguments in a string |
|
843 and pass it to the interepreter as a single argument. In this case, the |
|
844 following script: |
|
845 |
|
846 @example |
|
847 @group |
|
848 #! @var{octave-interpreter-name} -q -f # comment |
|
849 @end group |
|
850 @end example |
|
851 |
|
852 @noindent |
|
853 is equivalent to type at the command line: |
3294
|
854 |
6481
|
855 @example |
|
856 @group |
|
857 octave "-q -f # comment" |
|
858 @end group |
|
859 @end example |
3294
|
860 |
6481
|
861 @noindent |
|
862 which would obviously produce an error message. Unfortunately, it is |
|
863 impossible for Octave to know whether it has been called from the command |
|
864 line or from a @samp{#!} script, so some care is needed when using the |
|
865 @samp{#!} mechanism. |
|
866 |
|
867 Note that when Octave is started from an executable script, the built-in |
|
868 function @code{argv} returns a cell array containing the command line |
|
869 arguments passed to an executable Octave script, not the arguments |
|
870 passed to the Octave interpreter on the @samp{#!} line of the script. |
|
871 For example, the following program will reproduce the command line that |
|
872 is used to execute script, not @samp{-qf}. |
3294
|
873 |
|
874 @example |
|
875 @group |
|
876 #! /bin/octave -qf |
6479
|
877 printf ("%s", program_name ()); |
3294
|
878 for i = 1:nargin |
6479
|
879 arg_list = argv (); |
|
880 printf (" %s", arg_list@{i@}); |
3294
|
881 endfor |
|
882 printf ("\n"); |
|
883 @end group |
|
884 @end example |
|
885 |
4167
|
886 @node Comments |
3294
|
887 @section Comments in Octave Programs |
|
888 @cindex @samp{#} |
|
889 @cindex @samp{%} |
|
890 @cindex comments |
|
891 @cindex use of comments |
|
892 @cindex documenting Octave programs |
|
893 @cindex programs |
|
894 |
|
895 A @dfn{comment} is some text that is included in a program for the sake |
|
896 of human readers, and that is not really part of the program. Comments |
|
897 can explain what the program does, and how it works. Nearly all |
|
898 programming languages have provisions for comments, because programs are |
|
899 typically hard to understand without them. |
|
900 |
|
901 In the Octave language, a comment starts with either the sharp sign |
|
902 character, @samp{#}, or the percent symbol @samp{%} and continues to the |
|
903 end of the line. The Octave interpreter ignores the rest of a |
|
904 line following a sharp sign or percent symbol. For example, we could |
|
905 have put the following into the function @code{f}: |
|
906 |
|
907 @example |
|
908 @group |
|
909 function xdot = f (x, t) |
|
910 |
|
911 # usage: f (x, t) |
|
912 # |
|
913 # This function defines the right hand |
|
914 # side functions for a set of nonlinear |
|
915 # differential equations. |
|
916 |
|
917 r = 0.25; |
|
918 @dots{} |
|
919 endfunction |
|
920 @end group |
|
921 @end example |
|
922 |
|
923 The @code{help} command (@pxref{Getting Help}) is able to find the first |
|
924 block of comments in a function (even those that are composed directly |
|
925 on the command line). This means that users of Octave can use the same |
|
926 commands to get help for built-in functions, and for functions that you |
|
927 have defined. For example, after defining the function @code{f} above, |
|
928 the command @kbd{help f} produces the output |
|
929 |
|
930 @example |
|
931 @group |
|
932 usage: f (x, t) |
|
933 |
|
934 This function defines the right hand |
|
935 side functions for a set of nonlinear |
|
936 differential equations. |
|
937 @end group |
|
938 @end example |
|
939 |
|
940 Although it is possible to put comment lines into keyboard-composed |
|
941 throw-away Octave programs, it usually isn't very useful, because the |
|
942 purpose of a comment is to help you or another person understand the |
|
943 program at a later time. |
|
944 |