我想从命令行运行一些小的Erlang函数。
例如,要在python中打印日期,我可以使用:
python -c 'import time; print (time.strftime("%d/%m/%Y"))'
我希望能够为Erlang:类似
erl -s 'date().'
然而,我得到以下错误:
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
{"init terminating in do_boot",{undef,[{'date().',start,[],[]},{init,start_it,1,[]},{init,start_em,1,[]}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
可以从命令行运行小型Erlang脚本吗?
您可以使用eval
选项从命令行运行小型Erlang程序。例如:
erl -noinput -eval 'io:format("hello world~n").' -s init stop
有关命令行选项的更多详细信息,您可以阅读erl
手册页,但请简要介绍:
-noinput
表示运行时没有可读取的内容-eval
将其参数作为Erlang代码文本进行计算和执行-s init stop
执行init:stop/0
函数以关闭运行时
要打印当前日期和时间,您可以采用类似的方法:
erl -noinput -eval '{{Y,Mo,D},{H,Mi,S}} = calendar:now_to_local_time(os:timestamp()),
io:format("~4.4w-~2.2.0w-~2.2.0w ~2.2.0w:~2.2.0w:~2.2.0w~n",[Y,Mo,D,H,Mi,S]).' -s init stop
此代码使用os:timestamp/0
检索当前时间,通过calendar:now_to_local_time/1
将其转换为本地时间,然后使用io:format/2
对结果进行格式化,生成如下结果:
2014-10-22 10:09:53