[erlang-questions] shell question - import commands from external file

Klas Johansson klas.johansson@REDACTED
Tue Sep 22 21:14:33 CEST 2009


Hi,

> I have a newbie question.  In the shell, is there a way to
> import *shell* commands from an external file?

It's possible to add user-defined functions to the shell (which
can be called without the module name prefix).  Just create a
module called user_default.erl and make sure it's compiled and in
in the code path.  Here's a description from the shell [1] man
page:

    If a command (local function call) is not recognized by the
    shell, an attempt is first made to find the function in the
    module user_default, where customized local commands can be
    placed. If found, then the function is evaluated. Otherwise,
    an attempt is made to evaluate the function in the module
    shell_default. The module user_default must be explicitly
    loaded.

Here's a really short example:

1. Create user_default.erl:
     -module(user_default).
     -compile(export_all).
     foo() -> foo.
2. Compile it.
3. erl -pa <path-to-user-default>
4. 1> foo().
   foo

You will have to recompile and reload the file if you change it
though.

You can make sure the module is automatically loaded by adding it
to your ~/.erlang file as described in the man page for
shell_default [2] (then you wouldn't need the "-pa" option to erl):

    To add your own commands to the shell, create a module called
    user_default and add the commands you want. Then add the
    following line as the first line in your .erlang file in your
    home directory.

    code:load_abs("$PATH/user_default").

Note: You should not change the shell_default module, since this
implements the existing shell functions, but instead add your own
functions to user_default.

I don't know if this will help you or not, since it doesn't solve
all of the problems you listed, but perhaps it can prove useful?

[1] http://erlang.org/doc/man/shell.html
[2] http://www.erlang.org/doc/man/shell_default.html


Best Regards,
Klas


More information about the erlang-questions mailing list