escript(1)

NAME
escript - Erlang scripting support

DESCRIPTION

escript provides support for running short Erlang programs without having to compile them first and an easy way to retrieve the command line arguments.

EXPORTS

script-name script-arg1 script-arg2...
escript escript-flags script-name script-arg1 script-arg2...

    escript runs a script written in Erlang.

    Here follows an example.
    $ cat factorial
    #!/usr/bin/env escript
    %% -*- erlang -*-
    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

    Note that there should not be any module declaration in an Erlang script file. Instead, the first line is usually the interpreter line which invokes escript. If you invoke escript like this
    $ escript factorial 5

    the contents of the first line does not matter, but it cannot contain Erlang code as it will be ignored.

    If you know the location of the escript executable, the first line can directly give the path to escript. For instance:
    #!/usr/local/bin/escript

    As any other kind of scripts, Erlang scripts will not work on Unix platforms if they execution bit for the script file is not set. (Use chmod +x script-name to turn on the execution bit.)

    An Erlang script file must always contain the function main/1. When the script is run, the main/1 will be called with a list of strings representing the arguments given to the script (not changed or interpreted in any way).

    Call escript:script_name/0 from your to script to retrieve the pathname of the script (the pathname is usually, but not always, absolute).

    It is not necessary to export the main/1 function.

    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).

    Pre-processor directives in the script files are ignored, with the exception for the -include_lib directive. For instance, use
    -include_lib("kernel/include/file.hrl").

    to include the record definitions for the records used by the file:read_file_info/1 function.

    Pre-defined macros (such as ?MODULE) will not work. A script does not have module name, so BIFs such as spawn/3 that require a module name cannot be used. Instead, use a BIF that take a fun, such as spawn/1.

    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.

    If the main/1 function in the script returns successfully, the exit status for the script will be 0. If an exception is generated during execution, a short message will be printed and the script terminated with exit status 127.

    To return your own non-zero exit code, call halt(ExitCode); for instance:
    halt(1).

OPTIONS ACCEPTED BY ESCRIPT

    -s: Only perform a syntactic and semantic check of the script file. Warnings and errors (if any) are written to the standard output, but the script will not be run. The exit status will be 0 if there were no errors, and 127 otherwise.