escript(1)

πŸ’– NAME πŸ’–

escript β€” run a script written in Erlang, bestie. that's it. that's the whole thing. ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

script-name [arg1 arg2...]

πŸŽ€ DESCRIPTION πŸŽ€

okay SO. escript is literally the moment. it lets you run short Erlang programs without having to compile them first, and it gives you a super easy way to grab the command-line arguments. no build step, no drama, just vibes. πŸ’…

you make escripts either by writing them by hand (iconic) or by using escript:create/2 (also iconic).

escripts run by directly invoking them β€” tho heads up, this does not work on Windows, sorry Windows besties 😭:

script-name [arg1 arg2...]

or by calling the escript program itself, which works on all platforms:

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

for example, check out this absolute serve:

$ chmod u+x factorial
$ cat factorial

#!/usr/bin/env escript
%% -*- erlang -*-
%%! -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 up there is not like a normal Erlang module, okay? the first line is meant to be the interpreter line, the one that invokes escript. it's giving shebang.

but here's the tea: if you invoke the escript like this πŸ‘‡ the contents of the first line do not matter, except it cannot contain Erlang code because it will literally be ignored:

$ escript factorial 5

the second line in the example has an optional directive for the Emacs editor, which makes it enter the major mode for editing Erlang source files. if that directive is there, it must be on the second line. no negotiations. πŸŽ€

and if a comment selecting the encoding exists, it can live on the second line too.

NOTE: the encoding specified by that comment applies to the script itself. the encoding of the I/O-server, however, has to be set explicitly like so:
io:setopts([{encoding, latin1}])

the default encoding of the I/O-server for standard_io is unicode if it's supported. (see section Summary of Options) in the STDLIB User's Guide. πŸ’–

on the third line β€” or the second line, depending on whether that Emacs directive is present β€” you can specify arguments to the emulator, like:

%%! -sname factorial -mnesia debug verbose

that argument line must start with %%!, and the rest of the line gets interpreted as arguments to the emulator. periodt.

and if you know where the escript executable lives, the first line can just directly give the path to escript, like:

#!/usr/local/bin/escript

like any other type of script, Erlang scripts do not work on Unix platforms if the execution bit for the script file isn't set. (to turn on the execution bit, use chmod +x script-name.) 🫢

the remaining Erlang script file can contain Erlang source code, an inlined beam file, or an inlined archive file. pick your fighter.

an Erlang script file must always contain the main/1 function. when the script runs, main/1 gets called with a list of strings representing the arguments specified to the script β€” and those are not changed or interpreted in any way. raw. unfiltered. πŸ’…

if the main/1 function in the script returns successfully, the exit status for the script is 0. if an exception gets generated during execution, a short message is printed and the script terminates with exit status 127. 😭

to return your own non-zero exit code, call halt(ExitCode), for example:

halt(1).

to retrieve the pathname of the script, call escript:script_name/0 from your script. (the pathname is usually, but not always, absolute. she's mysterious like that.)

if the file contains source code (like in the example above), it gets processed by the epp preprocessor. which means you can, for example, use predefined macros (such as ?MODULE) and include directives like the -include_lib directive. for example, use

-include_lib("kernel/include/file.hrl").

to include the record definitions for the records used by function file:read_link_info/1. you can also select encoding by including an encoding comment right here, but if a valid encoding comment exists on the second line, it takes precedence. second line wins, sorry, those are the rules.

the script is checked for syntactic and semantic correctness before it's run. if there are warnings (such as unused variables), they get printed and the script will still be run. if there are errors, they get printed and the script will not be run and its exit status is 127.

both the module declaration and the export declaration of the main/1 function are optional. casual.

by default, the script will be compiled by the Erlang compiler. πŸ”₯

it's possible to force it to be interpreted instead by including the following line somewhere in the script file:

-mode(interpret).

execution of interpreted code is slower than compiled code, and some language constructs will not work, but there's no requirement for the Erlang compiler application to be available. trade-offs, bestie.

CHANGE: Before Erlang/OTP 27 the script would be interpreted by default. πŸ‘‘

🧚 Precompiled escripts 🧚

a script can also contain precompiled beam code. to create a precompiled escript, it's recommended that you use escript:create/2. in a precompiled script, the interpretation of the script header is the same as in a script containing source code. which means you can make a beam file executable by prepending the file with the lines starting with #! and %%! mentioned above. in a precompiled script, the main/1 function must be exported.

another option is to have an entire Erlang archive in the script. in an archive script, the interpretation of the script header is the same as in a script containing source code. which means you can make an archive file executable by prepending the file with the lines starting with #! and %%! mentioned above. in an archive script, the main/1 function must be exported. by default, the main/1 function in the module with the same name as the basename of the escript file is invoked. this behavior can be overridden by setting flag -escript main Module as one of the emulator flags. Module must be the name of a module that has an exported main/1 function. for more information about archives and code loading, see escript and code. πŸ’œ

it's often very convenient to have a header in the escript, especially on Unix platforms. however, the header is optional, so you can directly "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

look at her go. πŸ¦‹


🌈 Bundling escripts 🌈

it is possible to bundle escript(s) with an Erlang runtime system to make it self-sufficient and relocatable. in such a standalone system, the escript(s) should be located in the top bin directory of the standalone system and given .escript as file extension. further, the (built-in) escript program should be copied to the same directory and given the script's original name (without the .escript extension). this will enable use of the bundled Erlang runtime system. slay.

the (built-in) escript program first determines which Erlang runtime system to use and then starts it to execute your script. usually the runtime system is located in the same Erlang installation as the escript program itself. but for standalone systems with one or more escripts, it may be the case that the escript program in your path actually starts the runtime system bundled with the escript. this is intentional, and typically happens when the standalone system bin directory is not in the execution path (as it may cause its erl program to override the desired one) and the escript(s) are referred to via symbolic links from a bin directory in the path. iykyk. πŸ’…


✨ Options Accepted By escript ✨

  • -c β€” compiles the escript regardless of the value of the mode attribute. no thoughts, just compile.
  • -d β€” debugs the escript. starts the debugger, loads the module containing the main/1 function into the debugger, sets a breakpoint in main/1, and invokes main/1. if the module is precompiled, it must be explicitly compiled with option debug_info. πŸ”
  • -i β€” interprets the escript regardless of the value of the mode attribute. doing it live.
  • -s β€” performs 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 is 0 if any errors are found, otherwise 127. yes that's what it says, read it twice bestie πŸ’€
NOTE: the configuration of the Erlang emulator invoked by escript can also be controlled using the environment variables understood by erl. 🌸

that's the whole page, you ate it up fr fr. 🫢 this was Ericsson AB's moment β€” erts 17.0.6, escript(1), and that's on periodt. πŸ‘‘βœ¨

πŸ“¦ where this page comes from