如何从带有复杂参数的unix shell运行Erlang? [英] How to run Erlang from unix shell with complex params?

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

问题描述

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

I need to run complex Erlang module function from unix shell

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}

,但运行时

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?

推荐答案

我认为 escript 可能值得研究。

I think escript might be something worth looking into.

编辑:
一些示例。

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:创建一个名为 hello.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]).

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

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:设置执行位并运行

2: Set the execution bit and run

            $ chmod +x hello.escript
            $ ./hello.escript 
            Hello there bar@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  (abort with ^G)
            (foo@duval)1> Hello there bar@duval

这将创建一个外壳,在这种情况下可能不是您想要的。

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

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

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.

在完成这些示例并再次阅读您的问题之后,我认为我给出的可怕建议将是您的最佳选择,erl_call。我不喜欢问题标题中的复杂一词,其中imho手稿以易于阅读的方式允许更多复杂设置。 escript示例中的变量 _String 包含脚本的参数,该参数使您既可以通过shell访问输入,又可以在EVM中执行复杂的erlang操作。但是,如果您已经在某些其他应用程序中拥有逻辑并且只需要对erlang节点进行此简单调用,则erl_call可能会更直接。

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天全站免登陆