1.概述:

作为程序员对于脚本语言应该很熟悉了,脚本语言的优点很多,如快速开发、容易编写、实时开发和执行, 我们常用的脚本有Javascript、shell、python等,我们的erlang语言也有支持脚本运行的工具---escript,它支持在不编译的情况下,直接从命令行运行代码。

2. 示例:

编写了一个使用escript来解析application文件的脚本代码,为了读取vsn字段,如下:

 #!/usr/bin/env escript              %%% 和其他脚本语言一样,如果脚本未设置,将不可执行

 %% -*- erlang -*-
%%! -smp enable -sname app-test -mnesia debug verbose %%% erlang模拟器的参数 -module('app-test').
-mode(compile). %%%脚本语言一般都是解释性语言,而解释执行一般都比编译执行要慢,加上这一句,强制脚本编译。

main(AppFiles) -> %%% escript要求必须有main函数作为入口函数。
ConsistentRstate = lists:foldl(fun
(Rstate, nil) -> Rstate;
(Rstate, Rstate) -> Rstate;
(Rstate, PreviousRstate) ->
io:format(standard_error, "Inconsistent R-States: ~p and ~p~n", [Rstate, PreviousRstate]),
throw(error)
end,
nil,
lists:map(fun
(Filename) ->
{ok, [{application, _Application, Properties}]} = file:consult(Filename),
{vsn, Rstate} = lists:keyfind(vsn, 1, Properties),
Rstate
end,
AppFiles)),
io:format("~s~n", [ConsistentRstate]).
%%% appfile.appSrc 文件,是一个application文件

{application, test,
[
{id, "testId"},
{vsn, "version001"},
{modules, "&modules&"},
{mod, {sysApp, {sysAppCB, []}}},
{description, "this is a test application"},
{maxP, infinity}, % infinity | integer()
{maxT, infinity}, % infinity | integer()
{registered, []}, % [Name1, Name2|...]
{applications, []}, % [Appl1, Appl2|...]
{included_applications, []}, % [Appl1, Appl2|...]
{env, []}
]}.
 ~/test_tmp> chmod +x app-test.escript
~/test_tmp> ./app-test.escript appfile.appSrc
version001 也可以这样执行.erl .beam .zip ~/test_tmp> cat test.erl
-module(test).
-export([main/]). main([List]) ->
io:format("this is a test module: ~p~n", [List]). ~/test_tmp>
~/test_tmp> escript test.erl testtest
this is a test module: "testtest"
~/test_tmp>
~/test_tmp> erlc test.erl
~/test_tmp> escript test.beam testtest11
this is a test module: "testtest11"

以上是关于escript的简述。如有误,望指正。

erlang的脚本执行---escript的更多相关文章

  1. erlang虚拟机代码执行原理

     转载:http://blog.csdn.NET/mycwq/article/details/45653897 erlang是开源的,很多人都研究过源代码.但是,从erlang代码到c代码,这是个不小 ...

  2. 第9章 Shell基础(1)_Shell简介和脚本执行方式

    1. Shell概述 1.1 Shell简介 (1)Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编 ...

  3. MonoBehaviour Lifecycle(生命周期/脚本执行顺序)

    脚本执行顺序 前言 搭建一个示例来验证Unity脚本的执行顺序,大概测试以下部分: 物理方面(Physics) 渲染(Scene rendering) 输入事件(InputEvent) 流程图 Uni ...

  4. Shell文件权限和脚本执行

    一.预备知识 1.shell的作用   2.常识 (1)Tab键自动补全   使用Terminal时,输入命令的前几个字母,敲tab会自动补全命令或文件名.目录等. 好处:操作速度更快:不容易出错: ...

  5. python脚本执行Scapy出现IPv6警告WARNING解决办法

    安装完scapy,写了脚本执行后执行: WARNING: No route found for IPv6 destination :: (no default route?) 原因是用 from sc ...

  6. Linux脚本执行过程重定向

    Linux脚本执行过程重定向 一.bash调试脚本,并将执行过程重定向到指定文件 bash –x  shell.sh 2>&1 | tee shell.log

  7. PowerDesigner16.5 生成SQL脚本执行出错:collate chinese_prc_ci_as

    PowerDesigner16.5 生成SQL脚本执行出错, collate chinese_prc_ci_as 点DataBase-edit current dbms —— 左边Script - O ...

  8. sh脚本执行Java程序

    1.不引用Jar包或者资源文件夹 最简单的程序Hello World. 首先创建Hello.java public class Hello { public static void main(Stri ...

  9. crontab 中 python(cx_Oracle)脚本执行时需要用户环境变量,怎么办??

    import cx_Oracle Traceback (most recent call last): File "", line 1, in ? ImportError: lib ...

随机推荐

  1. The Singularity is Near---预测人工智能,科技走向的神书---奇点临近

    比尔盖茨评价本文作者: 雷·库兹韦尔是我所知道的预测人工智能未来最权威的人.他的这本耐人寻味的书预测未来信息技术得到空前发展,将促使人类超越自身的生物极限--以我们无法想象的方式超越我们的生命. 中文 ...

  2. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十)

    打开Arm.h,在其接口中添加一个新方法: -(void)armShoot; 接下来在Arm.m中实现该方法: -(void)armShoot{ CGPoint startPoint = [self ...

  3. Android之BaseAdapter的优雅实现

    在android的开发过程中,我们不可避免的要使用ListView来展示我们的Activity上面的内容.你可以使用很多种方式来实现这一功能,但是如何优雅快速的来实现呢?这就是我要写的了,既为了大家共 ...

  4. MySQL数据库写入图片并读取图片显示到JLabel上的详解

    相较于Oracle,MySQL作为一个轻量级的开源的数据库,可谓是大大简化了我们的操作.这次我就来写一个关于数据库存入图片,获取图片的例子吧,也为了今后的复习使用.(我们一般采取存入路径的方式,而不是 ...

  5. log4j.xml示例

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...

  6. 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组

    . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19913755 . 一. Android资源文件简介 1 ...

  7. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  8. 鹅场offer已Get,下周签约,终于能静下心来总结总结

    2015年9月20号下午,接到腾讯总部的电话,确定了offer相关信息,算是正式get了鹅场的offer,坐等下个周一周二的签约会. 心路篇 2015年2月:已经2月份了,自己在大学的时光已经来到了比 ...

  9. 2014新年福利,居然有人将Ext JS 4.1的文档翻译了

    原文:http://damoqiongqiu.iteye.com/blog/1998022

  10. table中 点击某一行变色

    效果图: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...