bash 启动 Erlang shell/节点时如何运行自定义函数?(即,`.erl` 文件中的函数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4292120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a custom function when starting an Erlang shell / node? (That is, a function within an `.erl` file)
提问by Ted Karmel
I can start an Erlang file either via the command line or bash script:
我可以通过命令行或 bash 脚本启动 Erlang 文件:
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...
任何建议表示赞赏...
回答by Jon Gretar
what you probably want is erl -s module_name function_name
你可能想要的是 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.
请注意,您永远不会像在示例中那样在 erl 命令中指定 erlang 文件。Erlang VM 加载代码路径中的所有模块。这包括本地目录。
From http://www.erlang.org/doc/man/erl.html:
从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).
-run Mod [Func [Arg1, Arg2, ...]](init flag) 使 init 调用指定的函数。Func 默认启动。如果没有提供参数,则假定函数的元数为 0。否则假定为元数 1,将列表 [Arg1,Arg2,...] 作为参数。所有参数都作为字符串传递。请参见 init(3)。
-s Mod [Func [Arg1, Arg2, ...]](init flag) 使 init 调用指定的函数。Func 默认启动。如果没有提供参数,则假定函数的元数为 0。否则假定为元数 1,将列表 [Arg1,Arg2,...] 作为参数。所有参数都作为原子传递。请参见 init(3)。
回答by toraritte
The erlman page][1] shows all the command line options, but [init(3)`seems to have the examples, and sometimes better descriptions. This posthas some good examples as well.
该erl手册页][1] shows all the command line options, but [的init(3)`似乎有例子,有时候更好的描述。这篇文章也有一些很好的例子。
Also, the options below are not mutually exclusive. The -run, -s, and -evalswitches can be mixed.
此外,以下选项并不相互排斥。的-run,-s和-eval开关可以是混合的。
Option 1: erl -runor erl -s
选项 1:erl -run或erl -s
The erlman pagedescribes the -sand -runswitches (the
texts are the same), but the examples are in init(3)(see blockquote below).
该erl手册页介绍-s和-run开关(文本相同),但这些例子中init(3)(见下文块引用)。
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).
警告:
被调用的模块必须已经被编译,否则 Erlang 运行时将在 init 时崩溃,产生一个神秘的错误消息(指出函数未定义的事实)。
-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.
-run Mod [Func [Arg1, Arg2, ...]]在系统初始化期间评估指定的函数调用。
Func默认为start. 如果没有提供参数,则假定函数的元数为 0。否则假定为元数 1,将列表[Arg1,Arg2,...]作为参数。所有参数都作为字符串传递。如果引发异常,Erlang 会停止并显示错误消息。例子:
% erl -run foo -run foo bar -run foo bar baz 1 2这将启动 Erlang 运行时系统并评估以下函数:
foo:start() foo:bar() foo:bar(["baz", "1", "2"]).这些函数在初始化过程中按顺序执行,然后正常终止并将控制权交给用户。这意味着
-run不返回的调用会阻止进一步处理;为了避免这种情况,在这种情况下使用一些 spawn 变体。
Option 2: erl -eval
选项 2: erl -eval
As mentioned in the section above, the module has to be compiled to be used with -runor -s, so either call erlcbefore, or add -evalto the mix. Assuming amod.erlis in the same folder where erlis executed
如上一节所述,必须编译模块才能与-run或一起使用-s,因此要么先调用erlc,要么添加-eval到混合中。假设amod.erl在erl执行的同一文件夹中
$ erl -eval 'compile:file(amod)' -run amod
This will drop to the Erlang shell prompt. See -noshell(erlman page) if only the Erlang runtime needs to be started up.
这将下降到 Erlang shell 提示符。如果只需要启动 Erlang 运行时,请参见-noshell(erl手册页)。
From init(3):
来自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.
-eval Expr
Expr在系统初始化期间扫描、解析和评估任意表达式。如果这些步骤中的任何一个失败(语法错误、解析错误或评估期间的异常),Erlang 将停止并显示错误消息。在以下示例中,Erlang 用作十六进制计算器:% erl -noshell -eval 'R = 16#1F+16#A0, io:format("~.16B~n", [R])' \ -s erlang halt BF如果
-eval指定了多个表达式,则按指定的顺序依次计算它们。-eval表达式按-s和-run函数调用顺序计算(这也按照指定的顺序)。与-s和 一样-run,不终止的评估会阻止系统初始化过程。

