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