从复制参数的unix shell运行erlang [英] run erlang from unix shell with complex params

查看:100
本文介绍了从复制参数的unix shell运行erlang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从unix shell运行复杂的erlang模块功能

  rpc:call('node@example.com',mnesia ,dirty_first,[mytable])

我该怎么办?



UPD:



我使 test.escript



chmod + x test.escript

  #!/ usr / lib64 / erlang / bin / escript 
%%! -name'test@example.com'
main(_Args) - >
R = rpc:call('node@example.com',mnesia,dirty_first,[mytable]),
io:format(〜p〜n,[R])。

并收到 {badrpc,nodedown} / p>

但运行时

  erl -name test@example.com 
1> rpc:call('node@example.com',mnesia,dirty_first,[mytable])。
{my,data}。

我的意思是它的工作原理,但是如何让escript工作正常?

解决方案

我认为 escript 可能是值得研究的东西。



编辑:
一些例子。



首先为所有示例:以某种方式启动远程节点。

  dannib @ duval:〜:> erl -sname bar 
(bar @ duval)1>二郎:get_cookie()。
'KNKKCFPYMJUPIOLYPOAA'

Escript



1:创建名为 hello.escript 的内容

 #!/ usr / bin / env escript 
%%! -sname foo @ duval -setcookie KNKKCFPYMJUPIOLYPOAA

main(_String) - >
Node ='bar @ duval',
Mod ='erlang',
Fun ='node',
Args = [],
R = rpc:call (Node,Mod,Fun,Args),
io:format(Hello there〜p〜n,[R])。

请注意, %%! -sname foo @ bar 标识主机上的节点(而不是创建nonode @ nohost),允许设置相同的cookie %%! -sname foo @ duvel -setcookie KNKKCFPYMJUPIOLYPOAA 作为目标主机,解决了获取 {badrpc,nodedown} 的问题。请注意,对于设置了节点名称和Cookie的以下示例(erl_call和-eval),相同的语句也适用。



2:设置执行位并运行

  $ chmod + x hello.escript 
$ ./hello.escript
你好吧吧@ duval

Erl_call



1:运行

  $ erl_call -c'KNKKCFPYMJUPIOLYPOAA'-a'erlang node'-n bar @ duval 
bar @ duval

评估



1:运行

  $ erl -sname foo -setcookie'KNKKCFPYMJUPIOLYPOAA'
-eval'io:format(Hello there〜p〜n,[rpc:call(bar @ duval,erlang,node, ])])'
... Eshell V5.7.4(中止与^ G)
(foo @ duval)1>你好,那里吧@ duval

这样就创建了一个shell,在这种情况下可能不是你想要的。



我可以提到,如果两个节点都在同一个主机上并使用相同的cookie默认值,则foo和bar的cookie值不必被显式设置就像在例子中。



在做了这些例子并再次阅读你的问题后,我想我所写的TERRIBLE ADVICE表示将是你最好的选择,erl_call。我以复杂这个词为题,其中imho escripts以易于阅读的方式允许更多的复杂设置。 escript示例中的变量 _String 保存脚本的参数,允许您通过shell访问输入,并在EVM中执行复杂的erlang操作。但是如果您在其他应用程序中已经有逻辑,并且只需要对erlang节点进行简单的调用,那么erl_call可能会更加直接。


i need run complex erlang module function from unix shell

rpc:call('node@example.com', mnesia, dirty_first, [mytable])

how can i do it?

UPD:

i make test.escript

chmod +x test.escript

#!/usr/lib64/erlang/bin/escript
%%! -name 'test@example.com'
main(_Args) ->
    R = rpc:call('node@example.com', mnesia, dirty_first, [mytable]),
    io:format("~p~n",[R]).

and receive {badrpc, nodedown}

but when run

erl -name test@example.com
1> rpc:call('node@example.com', mnesia, dirty_first, [mytable]).
{my, data}.

i mean it works, but howto make escript work proprely?

解决方案

I think escript might be something worth looking into.

Edit: Some examples.

First for all examples: Start the remote node somewhere, somehow.

            dannib@duval:~:> erl -sname bar
            (bar@duval)1> erlang:get_cookie().
            'KNKKCFPYMJUPIOLYPOAA'

Escript

1: Create a file named hello.escript with content

            #!/usr/bin/env escript
            %%! -sname foo@duval -setcookie KNKKCFPYMJUPIOLYPOAA

            main(_String) ->
                Node = 'bar@duval',
                Mod = 'erlang',
                Fun = 'node', 
                Args = [], 
                    R = rpc:call(Node, Mod, Fun, Args),
                io:format("Hello there ~p~n",[R]).

Notice that the %%! -sname foo@bar identifies the node on the host (instead of creating nonode@nohost), allow setting the same cookie %%! -sname foo@duvel -setcookie KNKKCFPYMJUPIOLYPOAA as target host which solves the problem of getting {badrpc,nodedown}. Notice that the same statement holds for the following examples (erl_call, and -eval) where both the node name and cookie is set.

2: Set the execution bit and run

            $ chmod +x hello.escript
            $ ./hello.escript 
            Hello there bar@duval

Erl_call

1: run

            $ erl_call -c 'KNKKCFPYMJUPIOLYPOAA' -a 'erlang node' -n bar@duval
            bar@duval

Eval

1: run

            $ erl -sname foo -setcookie 'KNKKCFPYMJUPIOLYPOAA' 
            -eval 'io:format("Hello there ~p~n",[rpc:call(bar@duval,erlang, node, [])])'
            ... Eshell V5.7.4  (abort with ^G)
            (foo@duval)1> Hello there bar@duval

This creates a shell which might not be what you want in this case.

I might mention that if both nodes are on the same host and using the same cookie default value, the cookie value for foo and bar don't have to be explicitly set like in the examples.

After doing these examples and reading your question again I think what I GIVE TERRIBLE ADVICE said will be your best choice, erl_call. I fell for the word "complex" in question title where imho escripts allow much more "complex" setups in a easy-to-read manner. The variable _String in the escript example holds the arguments to the script which allows you to both access input through shell and perform complex erlang operations in the EVM. But erl_call might be more straight forward if you already have logic in some other application and just need to make this simple call to an erlang node.

这篇关于从复制参数的unix shell运行erlang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆