Elixir交互式Shell: 3. 创建本地和远程Shell

Elixir交互式Shell: 1. 运行时系统标记
Elixir交互式Shell: 2. 常用命令
Elixir交互式Shell: 3. 创建本地和远程Shell
Elixir交互式Shell: 4. 处理文件和脚本
Elixir交互式Shell: 5. 配置IEx

这是IEx系列五部分中的第三部分, 在这一部分中, 我们将说明如何创建本地和远程IEx Shell.

用户切换菜单(CTRL + G)可以让我们对IEx Shell进行创建和管理.

键入CTRL + G进入用户切换菜单:

输入:

?, 显示可用的用户切换命令,

iex(4)> 
User switch command
 --> ?
 c [nn]            - connect to job
 i [nn]            - interrupt job
 k [nn]            - kill job
 j                 - list all jobs
 s [shell]         - start local shell
 r [node [shell]]  - start remote shell
 q                 - quit erlang
 ? | h             - this message
--> 

c, 连接到作业

在连接到作业之前, 我们创建一个本地变量foo

iex(2)> foo = "this is foo"
"this is foo"

键入CTRL + G进入用户切换带单

s, 启动本地Shell
j, 显示后台作业

 --> s
 --> j
   1  {erlang,apply,[#Fun<Elixir.IEx.CLI.1.116904714>,[]]}
   2* {shell,start,[]}

*标识当前作业, 键入c, 连接到该作业

 --> s
 --> j
   1  {erlang,apply,[#Fun<Elixir.IEx.CLI.1.116904714>,[]]}
   2* {shell,start,[]}
 --> c
Eshell V7.3  (abort with ^G)
1> 

上面是连接到本地的Erlang shell, 下面我们连接到IEx Shell

iex(1)> foo = "this is foo"
"this is foo"
iex(2)> 
User switch command
 --> s 'Elixir.IEx'
 --> c
Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 

现在我们已经在一个IEx Shell当中了. 在这个新创建的Shell中输入foo,结果如下

iex(1)> foo
** (CompileError) iex:1: undefined function foo/0

我们没有找到之前定义的foo, 因为这个新创建的SHELL是完全独立的, 现在我们切换回之前的SHELL

iex(1)> 
User switch command
 --> j
   1  {erlang,apply,[#Fun<Elixir.IEx.CLI.1.116904714>,[]]}
   2* {'Elixir.IEx',start,[]}
 --> c 1
                    # 注意这一行需要一个回车, 即一个空行
nil

我们再来看foo变量

iex(3)> foo
"this is foo"
iex(4)> 

r, 启动远程Shell

首先用--sname启动两个shell

# 本地Shell

➜  iex --sname local 
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(local@localhost)1> 

# 远程Shell

➜  iex --sname remote
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(remote@localhost)1> 

检查节点是否alive

iex(remote@localhost)1> Node.alive?
true
iex(remote@localhost)2> 

local@localhost连接到remote@localhost

User switch command
 --> r 'remote@localhost' 'Elixir.IEx'
 --> c
Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(remote@localhost)1> 

我们看到IEx提示符已经变成iex(remote@localhost)1>

在节点remote@localhost创建一个模块

iex(remote@localhost)2> defmodule Hello do
...(remote@localhost)2>   def foo, do: "this is foo"
...(remote@localhost)2> end
{:module, Hello,
 <<70, 79, 82, 49, 0, 0, 4, 220, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 126, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
 {:foo, 0}}

然后在local@localhost连接到的远程Shell中执行Hello.foo

iex(remote@localhost)1> Hello.foo
"this is foo"

成功!

练习:
使用--name local@127.0.0.1, --name remote@127.0.0.1进行测试

最后, 介绍最后一个方法连接到远程Shell, 该方法直接使用命令行的方式连接

➜  iex --sname local --remsh remote@localhost
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(remote@localhost)1> 

这里都是在本机进行测试的, 如果两个节点位于不同的主机上, 请确保两个主机的$HOME/.erlang.cookie文件有相同的Cookie值, 或者可以在启动时通过命令行选项 --cookie 指定

iex --cookie 1234567890 --sname remote
iex(remote@localhost)1> :erlang.get_cookie
:"1234567890"

iex --sname local --remsh remote@localhost --cookie 1234567890
iex(remote@localhost)1> :erlang.get_cookie
:"1234567890"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值