Can an escript invoke another escript function?

98 Views Asked by At

a.escript:

main(_)->
    b:read_config().

b.escript:

-module(b).
%% function exported
read_config()->
    %% read file
    io:format("hh~n").

If I convert the b.escript to b.erl and compile it to b.beam, it works. But I don't want to compile manually.

Maybe I can insert c:c(b) like this:

main()->
    c:c(b),
    b:read_config().

Can't auto load the b.escript when useing like b.eam?

1

There are 1 best solutions below

1
On

Yes, it is possible using compile:file/1

a.escript

%%This First line (i.e. the comment) MUST exist if we would like to call the script using 'escript <filename>', otherwise compilation error will occur. See 'https://www.erlang.org/doc/man/escript.html'
main([]) ->
    compile:file(b),
    b:read_config().

b.erl

-module(b).
-export([read_config/0]). %%Function MUST be exported, otherwise it will not be recognized from caller (e.g. escript )

read_config()->
    %% read file
    io:format("hh~n").

Call from Shell

$ escript a.escript
hh