1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE comref SYSTEM "comref.dtd">
<comref>
<header>
<copyright>
<year>2007</year><year>2010</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
The contents of this file are subject to the Erlang Public License,
Version 1.1, (the "License"); you may not use this file except in
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
</legalnotice>
<title>escript</title>
<prepared></prepared>
<docno></docno>
<date></date>
<rev></rev>
<file>escript.xml</file>
</header>
<com>escript</com>
<comsummary>Erlang scripting support</comsummary>
<description>
<p><c>escript</c> provides support for running short Erlang programs
without having to compile them first and an easy way to retrieve the
command line arguments.</p>
</description>
<funcs>
<func>
<name>script-name script-arg1 script-arg2...</name>
<name>escript escript-flags script-name script-arg1 script-arg2...</name>
<fsummary>Run a script written in Erlang</fsummary>
<desc>
<p><c>escript</c> runs a script written in Erlang.</p>
<p>Here follows an example.</p>
<pre>
$ <input>cat factorial</input>
#!/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).
$ <input>factorial 5</input>
factorial 5 = 120
$ <input>factorial</input>
usage: factorial integer
$ <input>factorial five</input>
usage: factorial integer </pre>
<p>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 <c>escript</c>. However if you
invoke the <c>escript</c> like this</p>
<pre>
$ <input>escript factorial 5</input> </pre>
<p>the contents of the first line does not matter, but it
cannot contain Erlang code as it will be ignored.</p>
<p>The second line in the example, contains an optional
directive to the <c>Emacs</c> editor which causes it to
enter the major mode for editing Erlang source files. If the
directive is present it must be located on the second
line.</p>
<p>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 </p>
<pre>
%%! -smp enable -sname factorial -mnesia debug verbose</pre>
<p>Such an argument line must start with <c>%%!</c> and the
rest of the line will interpreted as arguments to the emulator.</p>
<p>If you know the location of the <c>escript</c> executable, the first
line can directly give the path to <c>escript</c>. For instance:</p>
<pre>
#!/usr/local/bin/escript </pre>
<p>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 <c>chmod +x script-name</c> to turn on the execution bit.)
</p>
<p>The rest of the Erlang script file may either contain
Erlang <c>source code</c>, an <c>inlined beam file</c> or an
<c>inlined archive file</c>.</p>
<p>An Erlang script file must always contain the function
<em>main/1</em>. When the script is run, the
<c>main/1</c> function will be called with a list
of strings representing the arguments given to the script (not
changed or interpreted in any way).</p>
<p>If the <c>main/1</c> 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.</p>
<p>To return your own non-zero exit code, call <c>halt(ExitCode)</c>;
for instance:</p>
<pre>
halt(1).</pre>
<p>Call <seealso marker="#script_name_0">escript:script_name()</seealso>
from your to script to retrieve the pathname of the script
(the pathname is usually, but not always, absolute).</p>
<p>If the file contains source code (as in the example above),
it will be processed by the preprocessor <c>epp</c>. This
means that you for example may use pre-defined macros (such as
<c>?MODULE</c>) as well as include directives like
the <c>-include_lib</c> directive. For instance, use</p>
<pre>
-include_lib("kernel/include/file.hrl").</pre>
<p>to include the record definitions for the records used by the
<c>file:read_link_info/1</c> function.</p>
<p>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.</p>
<p>Both the module declaration and the export declaration of
the <c>main/1</c> function are optional.</p>
<p>By default, the script will be interpreted. You can force
it to be compiled by including the following line somewhere
in the script file:</p><pre>
-mode(compile).</pre>
<p>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.</p>
<p>As mentioned earlier, it is possible to have a script which
contains precompiled <c>beam</c> code. In a precompiled
script, the interpretation of the script header is exactly
the same as in a script containing source code. That means
that you can make a <c>beam</c> file executable by
prepending the file with the lines starting with <c>#!</c>
and <c>%%!</c> mentioned above. In a precompiled script, the
function
<c>main/1</c> must be exported.</p>
<p>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 <c>#!</c> and <c>%%!</c> mentioned
above. In an archive script, the function <c>main/1</c> must
be exported. By default the <c>main/1</c> function in the
module with the same name as the basename of the
<c>escript</c> file will be invoked. This behavior can be
overridden by setting the flag <c>-escript main Module</c>
as one of the emulator flags. The <c>Module</c> must be the
name of a module which has an exported <c>main/1</c>
function. See <seealso marker="kernel:code">code(3)</seealso>
for more information about archives and code loading.</p>
<p>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:</p>
<pre>
$ <input>escript factorial.erl 5</input>
factorial 5 = 120
$ <input>escript factorial.beam 5</input>
factorial 5 = 120
$ <input>escript factorial.zip 5</input>
factorial 5 = 120
</pre>
</desc>
</func>
<func>
<name>escript:create(FileOrBin, Sections) -> ok | {ok, binary()} | {error, term()}</name>
<fsummary>Create an escript</fsummary>
<type>
<v>FileOrBin = filename() | 'binary'</v>
<v>Sections = [Header] Body | Body</v>
<v>Header = shebang | {shebang, Shebang}
| comment | {comment, Comment}
| {emu_args, EmuArgs}</v>
<v>Shebang = string() | 'default' | 'undefined'</v>
<v>Comment = string() | 'default' | 'undefined'</v>
<v>EmuArgs = string() | 'undefined'</v>
<v>Body = {source, SourceCode}
| {beam, BeamCode}
| {archive, ZipArchive}</v>
<v>SourceCode = BeamCode = ZipArchive = binary()</v>
</type>
<desc>
<p>The <marker id="create_2"></marker> <c>create/2</c>
function creates an escript from a list of sections. The
sections can be given in any order. An escript begins with an
optional <c>Header</c> followed by a mandatory <c>Body</c>. If
the header is present, it does always begin with a
<c>shebang</c>, possibly followed by a <c>comment</c> and
<c>emu_args</c>. The <c>shebang</c> defaults to
<c>"/usr/bin/env escript"</c>. The comment defaults to
<c>"This is an -*- erlang -*- file"</c>. The created escript
can either be returned as a binary or written to file.</p>
<p>As an example of how the function can be used, we create an
interpreted escript which uses emu_args to set some emulator
flag. In this case it happens to disable the smp_support. We
do also extract the different sections from the newly created
script:</p>
<pre>
> <input>Source = "%% Demo\nmain(_Args) ->\n io:format(erlang:system_info(smp_support)).\n".</input>
"%% Demo\nmain(_Args) ->\n io:format(erlang:system_info(smp_support)).\n"
> <input>io:format("~s\n", [Source]).</input>
%% Demo
main(_Args) ->
io:format(erlang:system_info(smp_support)).
ok
> <input>{ok, Bin} = escript:create(binary, [shebang, comment, {emu_args, "-smp disable"},
{source, list_to_binary(Source)}]).</input>
{ok,<<"#!/usr/bin/env escript\n%% This is an -*- erlang -*- file\n%%!-smp disabl"...>>}
> <input>file:write_file("demo.escript", Bin).</input>
ok
> <input>os:cmd("escript demo.escript").</input>
"false"
> <input>escript:extract("demo.escript", []).</input>
{ok,[{shebang,default}, {comment,default}, {emu_args,"-smp disable"},
{source,<<"%% Demo\nmain(_Args) ->\n io:format(erlang:system_info(smp_su"...>>}]}
</pre>
<p>An escript without header can be created like this:</p>
<pre>
> <input>file:write_file("demo.erl",
["%% demo.erl\n-module(demo).\n-export([main/1]).\n\n", Source]).</input>
ok
> <input>{ok, _, BeamCode} = compile:file("demo.erl", [binary, debug_info]).</input>
{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,...>>}
> <input>escript:create("demo.beam", [{beam, BeamCode}]).</input>
ok
> <input>escript:extract("demo.beam", []).</input>
{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,...>>}]}
> <input>os:cmd("escript demo.beam").</input>
"true"
</pre>
<p>Here we create an archive script containing both Erlang
code as well as beam code. Then we iterate over all files in
the archive and collect their contents and some info about
them.
</p>
<pre>
> <input>{ok, SourceCode} = file:read_file("demo.erl").</input>
{ok,<<"%% demo.erl\n-module(demo).\n-export([main/1]).\n\n%% Demo\nmain(_Arg"...>>}
> <input>escript:create("demo.escript",
[shebang,
{archive, [{"demo.erl", SourceCode},
{"demo.beam", BeamCode}], []}]).</input>
ok
> <input>{ok, [{shebang,default}, {comment,undefined}, {emu_args,undefined},
{archive, ArchiveBin}]} = escript:extract("demo.escript", []).</input>
{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,...>>}]}
> <input>file:write_file("demo.zip", ArchiveBin).</input>
ok
> <input>zip:foldl(fun(N, I, B, A) -> [{N, I(), B()} | A] end, [], "demo.zip").</input>
{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"...>>}]}</pre>
</desc>
</func>
<func>
<name>escript:extract(File, Options) -> {ok, Sections} | {error, term()}</name>
<fsummary>Parses an escript and extracts its sections</fsummary>
<type>
<v>File = filename()</v>
<v>Options = [] | [compile_source]</v>
<v>Sections = Headers Body</v>
<v>Headers = {shebang, Shebang}
{comment, Comment}
{emu_args, EmuArgs}</v>
<v>Shebang = string() | 'default' | 'undefined'</v>
<v>Comment = string() | 'default' | 'undefined'</v>
<v>EmuArgs = string() | 'undefined'</v>
<v>Body = {source, SourceCode}
| {source, BeamCode}
| {beam, BeamCode}
| {archive, ZipArchive}</v>
<v>SourceCode = BeamCode = ZipArchive = binary()</v>
</type>
<desc>
<p>The <marker id="extract_2"></marker> <c>extract/2</c>
function parses an escript and extracts its sections. This is
the reverse of <c>create/2</c>.</p>
<p>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 <c>default</c>. If a section is missing, the extracted
value is set to the atom <c>undefined</c>. </p>
<p>The <c>compile_source</c> option only affects the result if
the escript contains <c>source</c> code. In that case the
Erlang code is automatically compiled and <c>{source,
BeamCode}</c> is returned instead of <c>{source,
SourceCode}</c>.</p>
<pre>
> <input>escript:create("demo.escript",
[shebang, {archive, [{"demo.erl", SourceCode},
{"demo.beam", BeamCode}], []}]).</input>
ok
> <input>{ok, [{shebang,default}, {comment,undefined}, {emu_args,undefined},
{archive, ArchiveBin}]} =
escript:extract("demo.escript", []).</input>
{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}]}
</pre>
</desc>
</func>
<func>
<name>escript:script_name() -> File</name>
<fsummary>Returns the name of an escript</fsummary>
<type>
<v>File = filename()</v>
</type>
<desc>
<p>The <marker id="script_name_0"></marker>
<c>script_name/0</c> function returns the name of the escript
being executed. If the function is invoked outside the context
of an escript, the behavior is undefined.</p>
</desc>
</func>
</funcs>
<section>
<title>Options accepted by escript</title>
<taglist>
<tag>-c</tag>
<item>Compile the escript regardless of the value of the mode attribute.
</item>
<tag>-d</tag>
<item>Debug the escript. Starts the debugger, loads the module
containing the <c>main/1</c> function into the debugger, sets a
breakpoint in <c>main/1</c> and invokes <c>main/1</c>. If the
module is precompiled, it must be explicitly compiled with the
<c>debug_info</c> option.
</item>
<tag>-i</tag>
<item>Interpret the escript regardless of the value of the mode attribute.
</item>
<tag>-s</tag>
<item>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.</item>
</taglist>
</section>
</comref>
|