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