Here follows an example.
$ cat factorial #!/usr/bin/env escript %% -*- erlang -*- %%! -smp enable -sname factorial -mnesia debug verbose main([String]) -> try N = list_to_integer(String), F = fac(N), io:format("factorial ~w = ~w\n", [N,F]) catch _:_ -> usage() end; main(_) -> usage(). usage() -> io:format("usage: factorial integer\n"), halt(1). fac(0) -> 1; fac(N) -> N * fac(N-1). $ factorial 5 factorial 5 = 120 $ factorial usage: factorial integer $ factorial five usage: factorial integer
The header of the Erlang script in the example differs from
a normal Erlang module. The first line is intended to be the
interpreter line, which invokes
$ escript factorial 5
the contents of the first line does not matter, but it cannot contain Erlang code as it will be ignored.
The second line in the example, contains an optional
directive to the
On the third line (or second line depending on the presence of the Emacs directive), it is possible to give arguments to the emulator, such as
%%! -smp enable -sname factorial -mnesia debug verbose
Such an argument line must start with
If you know the location of the
#!/usr/local/bin/escript
As any other kind of scripts, Erlang scripts will not work on
Unix platforms if the execution bit for the script file is not set.
(Use
The rest of the Erlang script file may either contain
Erlang
An Erlang script file must always contain the function
main/1. When the script is run, the
If the
To return your own non-zero exit code, call
halt(1).
Call
If the file contains source code (as in the example above),
it will be processed by the preprocessor
-include_lib("kernel/include/file.hrl").
to include the record definitions for the records used by the
The script will be checked for syntactic and semantic correctness before being run. If there are warnings (such as unused variables), they will be printed and the script will still be run. If there are errors, they will be printed and the script will not be run and its exit status will be 127.
Both the module declaration and the export declaration of
the
By default, the script will be interpreted. You can force it to be compiled by including the following line somewhere in the script file:
-mode(compile).
Execution of interpreted code is slower than compiled code. If much of the execution takes place in interpreted code it may be worthwhile to compile it, even though the compilation itself will take a little while.
As mentioned earlier, it is possible to have a script which
contains precompiled
As yet another option it is possible to have an entire
Erlang archive in the script. In a archive script, the
interpretation of the script header is exactly the same as
in a script containing source code. That means that you can
make an archive file executable by prepending the file with
the lines starting with
In many cases it is very convenient to have a header in the escript, especially on Unix platforms. But the header is in fact optional. This means that you directly can "execute" an Erlang module, beam file or archive file without adding any header to them. But then you have to invoke the script like this:
$ escript factorial.erl 5 factorial 5 = 120 $ escript factorial.beam 5 factorial 5 = 120 $ escript factorial.zip 5 factorial 5 = 120