I can start an Erlang file either via the command line or bash script:
exec erl file.erl
But, I cannot seem to find out how to directly start a function within this file.
e.g.
exec erl file.erl -f function()
Any suggestions appreciated...
I can start an Erlang file either via the command line or bash script:
exec erl file.erl
But, I cannot seem to find out how to directly start a function within this file.
e.g.
exec erl file.erl -f function()
Any suggestions appreciated...
what you probably want is erl -s module_name function_name
Note that you never specify the erlang file in the erl command like you did there in your example. The Erlang VM loads all modules in the codepath. That includes local directory.
From http://www.erlang.org/doc/man/erl.html:
-run Mod [Func [Arg1, Arg2, ...]] (init flag) Makes init call the specified function. Func defaults to start. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list [Arg1,Arg2,...] as argument. All arguments are passed as strings. See init(3).
-s Mod [Func [Arg1, Arg2, ...]] (init flag) Makes init call the specified function. Func defaults to start. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list [Arg1,Arg2,...] as argument. All arguments are passed as atoms. See init(3).
The erl man page][1] shows all the command line options, but [init(3)` seems to have the examples, and sometimes better descriptions. This post has some good examples as well.
Also, the options below are not mutually exclusive. The -run, -s, and -eval switches can be mixed.
erl -run or erl -sThe erl man page describes the -s and -run switches (the
texts are the same), but the examples are in init(3) (see blockquote below).
Caveat:
The called module has to be compiled already, otherwise the Erlang runtime will just crash on init, producing a cryptic error message (that points to the fact that the function is undefined).
-run Mod [Func [Arg1, Arg2, ...]]Evaluates the specified function call during system initialization.
Funcdefaults tostart. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list[Arg1,Arg2,...]as argument. All arguments are passed as strings. If an exception is raised, Erlang stops with an error message.Example:
% erl -run foo -run foo bar -run foo bar baz 1 2This starts the Erlang runtime system and evaluates the following functions:
foo:start() foo:bar() foo:bar(["baz", "1", "2"]).The functions are executed sequentially in an initialization process, which then terminates normally and passes control to the user. This means that a
-runcall that does not return blocks further processing; to avoid this, use some variant of spawn in such cases.
erl -evalAs mentioned in the section above, the module has to be compiled to be used with -run or -s, so either call erlc before, or add -eval to the mix. Assuming amod.erl is in the same folder where erl is executed
$ erl -eval 'compile:file(amod)' -run amod
This will drop to the Erlang shell prompt. See -noshell (erl man page) if only the Erlang runtime needs to be started up.
From init(3):
-eval ExprScans, parses, and evaluates an arbitrary expression
Exprduring system initialization. If any of these steps fail (syntax error, parse error, or exception during evaluation), Erlang stops with an error message. In the following example Erlang is used as a hexadecimal calculator:% erl -noshell -eval 'R = 16#1F+16#A0, io:format("~.16B~n", [R])' \\ -s erlang halt BFIf multiple
-evalexpressions are specified, they are evaluated sequentially in the order specified.-evalexpressions are evaluated sequentially with-sand-runfunction calls (this also in the order specified). As with-sand-run, an evaluation that does not terminate blocks the system initialization process.