IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

Erlang Shell实用小技巧

Erlang非业余研究 2011-10-14 13:42:35 累计浏览 2,079 次
本机暂存
    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. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. gen_tcp发送进程被挂起起因分析及对策 (累计阅读 37,821)
  2. 调试工具之GDB (累计阅读 14,829)
  3. 强制刷新本地 DNS 缓存记录 (累计阅读 10,917)
  4. Linux date 命令获取某日期的前一天 (累计阅读 9,881)
  5. ps 命令常见用法 (累计阅读 9,500)
  6. linux下搜索find命令详解 (累计阅读 9,459)
  7. Bash 小技巧:给目录加上书签,快速切换目录 (累计阅读 8,088)
  8. linux下的高效代码搜索工具-ack (累计阅读 6,652)
  9. 统计最近用过的linux命令 (累计阅读 6,533)
  10. gen_tcp发送缓冲区以及水位线问题分析 (累计阅读 6,062)