escript Erlang scripting support, 可以让erl模块转身变成unix script来使用,大大方便用户,具体的使用参看otp文档。我这里要演示的是些比较被忽视的高级特性:
首先crack erts/etc/common/escript.c:33 static int debug = 1; 让之显示调用参数。
root@nd-desktop:~# cat >factorial
#!/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).
CTRL+D
root@nd-desktop:~# chmod +x factorial
root@nd-desktop:~# ./factorial 10
erl +B -boot start_clean -noshell -smp enable -sname factorial -mnesia debug verbose -run escript start -extra ./factorial 10
factorial 10 = 3628800
特性1:
摘抄文档。。。
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
%%! -smp enable -sname factorial -mnesia debug verbose
Such an argument line must start with %%! and the rest of the line will interpreted as arguments to the emulator.
我们可以看到 这些选项被传递给了 erl
特性2:
-mode(compile).
这个选项是在escript.erl这个模块处理的。默认情况下 escript是被解释执行的,如果你的脚本很复杂,那么效率估计会是瓶颈。这种情况下, 你可以通过这个选项来让escript来先编译你的模块成opcode, 在vm里面运行。
特性3:
-d 选项 用来调试script的
-c 编译执行
-i 解释执行
-s 只检查不执行
root@nd-desktop:~# escript -d ./factorial 10
我们就可以看到 调试界面如下图
特性4:
可以把一个beam文件作为script
root@nd-desktop:/usr/src# cat hello.erl
-module(hello).
-export([start/0,main/1]).
main(_)->
start().
start()->
io:format("hello world~n",[]).
root@nd-desktop:/usr/src# erlc hello.erl
root@nd-desktop:/usr/src# cat >hello
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
CTRL+D
root@nd-desktop:/usr/src# cat hello.beam >>hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world
特性5:
可以把一个zip文件作为script
root@nd-desktop:/usr/src# cat hello.erl
-module(hello).
-export([start/0,main/1]).
main(_)->
start().
start()->
io:format("hello world, fac(10)=~w ~n",[fac:fac(10)]).
root@nd-desktop:/usr/src# cat fac.erl
-module(fac).
-export([fac/1]).
fac(0) ->
1;
fac(N) -> N * fac(N-1).
root@nd-desktop:/usr/src# erlc *.erl
root@nd-desktop:/usr/src# erl
Erlang R13B03 (erts-5.7.4) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4 (abort with ^G)
1> zip:zip("hello.zip", ["hello.beam", "fac.beam"]).
{ok,"hello.zip"}
2>
root@nd-desktop:/usr/src# cat >hello
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose -escript main hello
CTRL+D
root@nd-desktop:/usr/src# cat hello.zip >>hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world, fac(10)=3628800
特性6:
在独立的包里面 把escript伪装成我们的应用程序
root@nd-desktop:/usr/src# cat >hello.escript
-module(hello).
-export([start/0,main/1]).
main(_)->
start().
start()->
io:format("hello world~n",[]).
CTRL+D
root@nd-desktop:/usr/src# cp `which escript` hello
root@nd-desktop:/usr/src# ./hello
hello world
规则是 escript 改名成xxxx 执行xxxx的时候 实际上要读取的脚本是 xxxx.escript
综述: escript是很强大的 未来的erlang standalone全靠它。
- 大小: 68.6 KB
分享到:
- 2009-11-25 05:42
- 浏览 5851
- 评论(5)
- 论坛回复 / 浏览 (2 / 2075)
- 查看更多
相关推荐
eScript Language Reference
Siebel eScript 优化实战、实例介绍。
oracel support官方提供的siebel escript最佳实践以及其他人总结的siebel escript最佳实践.(资料是英文的)
Siebel eScript Siebel eScriptSiebel eScriptSiebel eScriptSiebel eScript
嵌入在注释中的脚本并在运行时更改了 txt 文件。
erlang-script.rpm 安装mq前的依赖 来源:https://packagecloud.io/rabbitmq/erlang/install#bash-rpm
六角escript格式的Hex.pm集成。联系我们如果在使用此库时发现任何错误或有问题,请在此存储库中(或拉取请求:))。 您可以在检查我们所有的开源项目。建造打造hexer只是运行make escript 。 这将编译项目并在bin/...
windows脚本技术script
类似rabitmq消息队列,可订阅消息发布消息,常用语物联网项目,mqtt类似rabitmq消息队列,可订阅消息发布消息,常用语物联网项目,mqtt
siebel ag 是siebel 的入门教程,请大家笑纳
主要对javascript的数组方法进行了详细的汇总,包括了最常用的的数组方法,还有扩展方法,很全面,感兴趣的小伙伴们可以参考一下
一维的doa角度估计escript程序,利用的是LS-ESCRIPT算法
emqx开源版本, 已经编译好, 包含 emqx_bridge_kafka插件(来源于github上的开源项目), 把消息桥接到kafka.
windows server2012r2安装emqx服务器报错script.exe错误 打不开console解决办法 安装c++库
emqx
Siebel中文培训文档。 0 Step By Step-客户端安装和配置.pdf 1 Step By Step-Table.pdf 2 Step By Step-Business Compoent & Join.pdf 3 Step By Step-Pick List.pdf ...10 Step By Step-eScript.pdf
Sequel.js-ORM @sequeljs/orm是在JavaScript的@sequeljs/ast和@sequeljs/omi之上构建的ORM。安装npm install -- save @ sequeljs / orm贡献随时为这个库做贡献。 有关更多信息,请参阅我们的。执照灵感@sequeljs/orm...
技术要点:【vue-router+axios-api+element-ui+BaiduMap+echart+escript6】
WindowsServer2012 x64部署emqx,PowerShell start 时提示 escript.ext "停止工作",解决安装包。
这将生成一个名为ex_top的escript可执行文件,可以通过键入./ex_top来执行。 $ git clone https://github.com/utkarshkukreti/ex_top $ cd ex_top $ mix escript.build $ ./ex_top 用法 键盘快捷键 钥匙 用 j或向...