escript

A simple one pass "load and go"

Erlang scripting interface

Joe Armstrong
Robert Virding

3 - April - 2001

Last revised 20 - February - 2004 - tested with R11-B3 on Solaris with a bash shell.

### Installation

1. Fetch escript-4.1.tgz
2. Unpack.
3. Type make.
4. Move the file escript to somewhere in your path.

### Testing

    ./fibc abc
    Usage fib
    > ./factorial abc
    Usage factorial <Int>
    > ./fibc 20
    fib 20 = 10946

The fibi script is as follows:

    #!/usr/bin/env escript

    %% Example of an interpreted script

    %% Usage:
    %%   fib

    -export([main/1]).

    main([X]) ->
        case (catch list_to_integer(X)) of
    	{'EXIT', _} ->
    	    usage();
    	J ->
    	    N = fib(J),
    	    io:format("fib ~w = ~w~n",[J, N])
        end;
    main(_) ->
        usage().

    usage() ->
        io:format("Usage fib ~n").

    fib(0) -> 1;
    fib(1) -> 1;
    fib(N) -> fib(N-1) + fib(N-2).

### Compiled or Interpreted code?

The default mode of escript is to interpret the code. By adding the attribute:

    -mode(compile).

Will cause the code in the script to be compiled, instead of interpreted. Note that for many scripts interpreting the code is much faster than compiling the code. We can time the code as follows:

    $ time fibc 20
    fib 20 = 10946

    real    0m1.041s
    user    0m0.540s
    sys     0m0.280s

Here is a table comparing fibi (fibonacci interpreted) with fibc (compiled):

EXpression | time (secs)
fibi 20 | 0.93
fibc 20 | 1.041
fibc 30 | 1.36
fibi 30 | 38.90

You have to decide for yourself which is faster.

### Release Notes

The origonal version was writen by Joe Armstrong and Robert Virding probably on 3 april 2001.

Up to version-3.0 needed a slightly hacked version of erl_eval.erl. These changes are now integrated into the official distribution.

### Bugs

- 8 years too late :-)
- Imports, exports macros and includes are not understood.
- The escaping in the the escript script works in bash - but maybe not in sh.

### Versions

- escript-2.0 Added suggestion by Luke Gorrie on quoting arguments. Added -mode(compile).
- escript-3.0 Added Fix by Bengt Kleberg (should be `#! /bin/sh` not `#!/bin/sh`).