技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 其他 --> Erlang Shell实用小技巧

Erlang Shell实用小技巧

浏览:1440次  出处信息
    Erlang Shell下有很多内置的命令,在平时交互的时候很好用,文档里面都是一行带过,大家可能没什么感觉。

     我来重点讲解和演示下:

$ erl
Erlang R14B04 (erts-5.8.5) 1 [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.5  (abort with ^G)
1> help().
** shell internal commands **
b()        -- display all variable bindings
e(N)       -- repeat the expression in query 
f()        -- forget all variable bindings
f(X)       -- forget the binding of variable X
h()        -- history
history(N) -- set how many previous commands to keep
results(N) -- set how many previous command results to keep
catch_exception(B) -- how exceptions are handled
v(N)       -- use the value of query 
rd(R,D)    -- define a record
rf()       -- remove all record information
rf(R)      -- remove record information about R
rl()       -- display all record information
rl(R)      -- display record information about R
rp(Term)   -- display Term using the shell\'s record information
rr(File)   -- read record information from File (wildcards allowed)
rr(F,R)    -- read selected record information from file(s)
rr(F,R,O)  -- read selected record information with options

    我最经常用的有以下几个,熟悉了感觉就很爽:

    

     抹掉变量:

    f() - forget all variables

     f(X) - forget X

1> Pid=spawn(fun()-> receive _->ok end end).
<0.33.0>
2>
2> Pid=spawn(fun()-> receive _->ok end end).
** exception error: no match of right hand side value <0.35.0>
3> f(Pid).
ok
4> Pid=spawn(fun()-> receive _->ok end end).
<0.39.0>
5> f().
ok
6> Pid=spawn(fun()-> receive _->ok end end).
<0.42.0>
7>

    找回命令执行结果:

    v(42) - recall result from line 42

     v(-1) - recall result from previous line

$ erl
1> spawn(fun()-> receive _->ok end end). %%忘记赋值,有spawn这样的命令有副作用,不能重复执行
<0.33.0>
2> Pid=v(-1).  %% 补救
<0.33.0>
3> v(2).
<0.33.0>

    读入记录定义:

    rr(foo) - read record definitions from module foo

$ touch test.dat
$ locate file.hrl
/usr/local/lib/erlang/lib/kernel-2.14/include/file.hrl
$ erl
1> file:read_file_info("test.dat").  %%信息很乱
{ok,{file_info,38914,regular,read_write,
               {{2011,10,5},{14,56,49}},
               {{2011,10,5},{14,56,49}},
               {{2011,10,5},{14,56,49}},
               33261,1,234881026,0,4577608,501,20}}
2>
2> rr("/usr/local/lib/erlang/lib/kernel-2.14/include/file.hrl").
[file_descriptor,file_info]
3>
3> file:read_file_info("test.dat"). %%自描述
{ok,#file_info{size = 38914,type = regular,
               access = read_write,
               atime = {{2011,10,5},{14,56,49}},
               mtime = {{2011,10,5},{14,56,49}},
               ctime = {{2011,10,5},{14,56,49}},
               mode = 33261,links = 1,major_device = 234881026,
               minor_device = 0,inode = 4577608,uid = 501,gid = 20}}
4>

    找回命令历史记录:

    h()

$ erl
1> X=1.
1
2> Y=2.
2
3> Z=3.
3
4> h().
1: X = 1
-> 1
2: Y = 2
-> 2
3: Z = 3
-> 3
ok
5>

    祝大家玩得开心!

建议继续学习:

  1. Erlang match_spec引擎介绍和应用    (阅读:4577)
  2. whatsapp深度使用Erlang有感    (阅读:4633)
  3. php-erlang    (阅读:4319)
  4. gen_tcp调用进程收到{empty_out_q, Port}消息奇怪行为分析    (阅读:3555)
  5. hibernate使用注意事项    (阅读:3228)
  6. Erlang linkin driver用port_control方式时的一些经验分享    (阅读:2975)
  7. Erlang如何限制节点对集群的访问之net_kernel:allow    (阅读:3028)
  8. ERLANG OTP源码分析 – gen_server    (阅读:2871)
  9. erlang学习手记    (阅读:2703)
  10. gen_tcp容易误用的一点解释    (阅读:2647)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1