Home > Erlang探索 > Erlang Shell实用小技巧

Erlang Shell实用小技巧

October 5th, 2011

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: Erlang Shell实用小技巧

Erlang Shell下有很多内置的命令,在平时交互的时候很好用,文档里面都是一行带过,大家可能没什么感觉。
我来重点讲解和演示下:

$ erl
Erlang R14B04 (erts-5.8.5) [source][/source][/source] [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 <N>
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 <N>
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> 

祝大家玩得开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Erlang探索 Tags:
  1. 空杯岳
    October 5th, 2011 at 16:52 | #1

    学习了!

  2. piboyeliu
    February 1st, 2012 at 17:12 | #2

    erl 怎么支持 history啊, bash 和 ruby的irb都支持。

    Yu Feng Reply:

    可以用第三方的readline编辑工具,取代erlang标准readline功能。 再说erlang本身也有history功能,具体参看h().

Comments are closed.