It is possible to bundle
The (built-in)
Example:
$ chmod u+x factorial $ 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
However, if you invoke the
$ escript factorial 5
The second line in the example contains an optional
directive to the
If a comment selecting the
The encoding specified by the above mentioned comment applies to the script itself. The encoding of the I/O-server, however, must be set explicitly as follows:
io:setopts([{encoding, unicode}])
The default encoding of the I/O-server for
On the third line (or second line depending on the presence of the Emacs directive), arguments can be specified to the emulator, for example:
%%! -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 type of scripts, Erlang scripts do not work on
Unix platforms if the execution bit for the script file is not set.
(To turn on the execution bit, use
The remaining Erlang script file can either contain Erlang source code, an inlined beam file, or an inlined archive file.
An Erlang script file must always contain the
If the
To return your own non-zero exit code, call
halt(1).
To retrieve the pathname of the script, call
If the file contains source code (as in the example above),
it is processed by the
-include_lib("kernel/include/file.hrl").
to include the record definitions for the records used by function
The script is checked for syntactic and semantic
correctness before it is run. If there are warnings (such as
unused variables), they are printed and the script will
still be run. If there are errors, they are printed and
the script will not be run and its exit status is
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
can be worthwhile to compile it, although the compilation
itself takes a little while. Also,
As mentioned earlier, a script can
contains precompiled
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. This means that you can
make an archive file executable by prepending the file with
the lines starting with
It is often very convenient to have a header in the escript, especially on Unix platforms. However, the header is optional, so 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 as follows:
$ escript factorial.erl 5 factorial 5 = 120 $ escript factorial.beam 5 factorial 5 = 120 $ escript factorial.zip 5 factorial 5 = 120
As an example of how the function can be used, we create an
interpreted escript that uses
> Source = "%% Demo\nmain(_Args) ->\n io:format(erlang:system_info(smp_support)).\n". "%% Demo\nmain(_Args) ->\n io:format(erlang:system_info(smp_support)).\n" > io:format("~s\n", [Source]). %% Demo main(_Args) -> io:format(erlang:system_info(smp_support)). ok > {ok, Bin} = escript:create(binary, [shebang, comment, {emu_args, "-smp disable"}, {source, list_to_binary(Source)}]). {ok,<<"#!/usr/bin/env escript\n%% This is an -*- erlang -*- file\n%%!-smp disabl"...>>} > file:write_file("demo.escript", Bin). ok > os:cmd("escript demo.escript"). "false" > escript:extract("demo.escript", []). {ok,[{shebang,default}, {comment,default}, {emu_args,"-smp disable"}, {source,<<"%% Demo\nmain(_Args) ->\n io:format(erlang:system_info(smp_su"...>>}]}
An escript without header can be created as follows:
> file:write_file("demo.erl", ["%% demo.erl\n-module(demo).\n-export([main/1]).\n\n", Source]). ok > {ok, _, BeamCode} = compile:file("demo.erl", [binary, debug_info]). {ok,demo, <<70,79,82,49,0,0,2,208,66,69,65,77,65,116,111,109,0,0,0, 79,0,0,0,9,4,100,...>>} > escript:create("demo.beam", [{beam, BeamCode}]). ok > escript:extract("demo.beam", []). {ok,[{shebang,undefined}, {comment,undefined}, {emu_args,undefined}, {beam,<<70,79,82,49,0,0,3,68,66,69,65,77,65,116, 111,109,0,0,0,83,0,0,0,9,...>>}]} > os:cmd("escript demo.beam"). "true"
Here we create an archive script containing both Erlang code and Beam code, then we iterate over all files in the archive and collect their contents and some information about them:
> {ok, SourceCode} = file:read_file("demo.erl"). {ok,<<"%% demo.erl\n-module(demo).\n-export([main/1]).\n\n%% Demo\nmain(_Arg"...>>} > escript:create("demo.escript", [shebang, {archive, [{"demo.erl", SourceCode}, {"demo.beam", BeamCode}], []}]). ok > {ok, [{shebang,default}, {comment,undefined}, {emu_args,undefined}, {archive, ArchiveBin}]} = escript:extract("demo.escript", []). {ok,[{shebang,default}, {comment,undefined}, {emu_args,undefined}, {{archive,<<80,75,3,4,20,0,0,0,8,0,118,7,98,60,105, 152,61,93,107,0,0,0,118,0,...>>}]} > file:write_file("demo.zip", ArchiveBin). ok > zip:foldl(fun(N, I, B, A) -> [{N, I(), B()} | A] end, [], "demo.zip"). {ok,[{"demo.beam", {file_info,748,regular,read_write, {{2010,3,2},{0,59,22}}, {{2010,3,2},{0,59,22}}, {{2010,3,2},{0,59,22}}, 54,1,0,0,0,0,0}, <<70,79,82,49,0,0,2,228,66,69,65,77,65,116,111,109,0,0,0, 83,0,0,...>>}, {"demo.erl", {file_info,118,regular,read_write, {{2010,3,2},{0,59,22}}, {{2010,3,2},{0,59,22}}, {{2010,3,2},{0,59,22}}, 54,1,0,0,0,0,0}, <<"%% demo.erl\n-module(demo).\n-export([main/1]).\n\n%% Demo\nmain(_Arg"...>>}]}
All sections are returned even if they do not exist in the
escript. If a particular section happens to have the same
value as the default value, the extracted value is set to the
atom
Option
Example:
> escript:create("demo.escript", [shebang, {archive, [{"demo.erl", SourceCode}, {"demo.beam", BeamCode}], []}]). ok > {ok, [{shebang,default}, {comment,undefined}, {emu_args,undefined}, {archive, ArchiveBin}]} = escript:extract("demo.escript", []). {ok,[{{archive,<<80,75,3,4,20,0,0,0,8,0,118,7,98,60,105, 152,61,93,107,0,0,0,118,0,...>>} {emu_args,undefined}]}