Difference between revisions of "BASH and Octave"

From Octave
Jump to navigation Jump to search
(Created page with "One of the nice features of Octave (and similar languages) is that you don't have to compile the code that you generate. It is interpreted by Octave instead. This make learnin...")
 
Line 1: Line 1:
 
One of the nice features of Octave (and similar languages) is that you don't have to compile the code that you generate. It is interpreted by Octave instead. This make learning the language easy by trail-and-error. Very often it is useful to save what you have learned from you fiddlings during the current Octave 'session' that you have opened for this fiddling.
 
One of the nice features of Octave (and similar languages) is that you don't have to compile the code that you generate. It is interpreted by Octave instead. This make learning the language easy by trail-and-error. Very often it is useful to save what you have learned from you fiddlings during the current Octave 'session' that you have opened for this fiddling.
Octave keeps a copy of all your command you give on the Octave command line. In Linux it is saved in the file {{~/.octave_hist}}.
+
Octave keeps a copy of all your command you give on the Octave command line. In Linux it is saved in the file {{Codeline|~/.octave_hist}}.
 +
 
 +
If you want to save all the Octave commands you gave during the last Octave session, generate a file {{Codeline|octave_save_last_session}} in your directory {{Codeline|~/bin}} and put the following inside:
 +
(that is: you have to open a terminal on your Linux computer and type the following commands on the terminal command line (make sure it is the BASH shell).
 +
 
 +
{{Code||<syntaxhighlight lang="bash" style="font-size:13px">
 +
#!/bin/bash                                                                                                                                                                               
 +
filename='octave_save_last_session.m'
 +
if [[ $# -eq 1 ]] ; then
 +
        filename=$1
 +
fi
 +
 
 +
tac $HOME/.octave_hist | awk '/^#/ {found++} ; found<2 ' |tac >$filename
 +
</syntaxhighlight>}}
 +
 
 +
Then you still need to tell the BASH shell to make this file {{Codeline|~/bin/octave_save_last_session}} executable:
 +
 
 +
{{Code||<syntaxhighlight lang="bash" style="font-size:13px">
 +
chmod u+x ~/bin/octave_save_last_session
 +
</syntaxhighlight>}}
 +
 
 +
Now, when you have run Octave, have done something in that session that you want to save to a script (a {{Codeline|*.m}} file), then, on the command line, you just type {{Codeline|octave_save_last_session}}, and the last session is copied from the {{Codeline|~/.octave_hist}} file and saved to {{Codeline|octave_save_last_session.m}}. You can edit that file to remove parts you don't want to save, or you can re-execute the commands by {{Codeline|octave --persist octave_save_last_session.m}}.

Revision as of 08:51, 7 February 2013

One of the nice features of Octave (and similar languages) is that you don't have to compile the code that you generate. It is interpreted by Octave instead. This make learning the language easy by trail-and-error. Very often it is useful to save what you have learned from you fiddlings during the current Octave 'session' that you have opened for this fiddling. Octave keeps a copy of all your command you give on the Octave command line. In Linux it is saved in the file ~/.octave_hist.

If you want to save all the Octave commands you gave during the last Octave session, generate a file octave_save_last_session in your directory ~/bin and put the following inside: (that is: you have to open a terminal on your Linux computer and type the following commands on the terminal command line (make sure it is the BASH shell).

#!/bin/bash                                                                                                                                                                                 
filename='octave_save_last_session.m'
if [[ $# -eq 1 ]] ; then
        filename=$1
fi

tac $HOME/.octave_hist | awk '/^#/ {found++} ; found<2 ' |tac >$filename

Then you still need to tell the BASH shell to make this file ~/bin/octave_save_last_session executable:

chmod u+x ~/bin/octave_save_last_session

Now, when you have run Octave, have done something in that session that you want to save to a script (a *.m file), then, on the command line, you just type octave_save_last_session, and the last session is copied from the ~/.octave_hist file and saved to octave_save_last_session.m. You can edit that file to remove parts you don't want to save, or you can re-execute the commands by octave --persist octave_save_last_session.m.