Erlang Shell實用小技巧
阿新 • • 發佈:2018-12-31
Erlang Shell下有很多內建的命令,在平時互動的時候很好用,文件裡面都是一行帶過,大家可能沒什麼感覺。
我來重點講解和演示下:$ erl Erlang R14B04 (erts-5.8.5) 1[/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我最經常用的有以下幾個,熟悉了感覺就很爽: 抹掉變數: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>祝大家玩得開心!