从外部erlang文件获取shell中的返回值
我在erlang中有一个启动一些模块的脚本文件。在Erlangshell中,我想使用start函数返回的对象 我有我的档案:从外部erlang文件获取shell中的返回值,shell,erlang,Shell,Erlang,我在erlang中有一个启动一些模块的脚本文件。在Erlangshell中,我想使用start函数返回的对象 我有我的档案: -module(myfile). main() -> %% do some operations MyReturnVar. 我想为最终用户提供操作MyReturnVar变量的最简单方法。在shell脚本中,我执行执行shell中函数的$erl-s myfile main 有没有办法在shell中获取MyReturnVar 另一种方法是直接从shel
-module(myfile).
main() ->
%% do some operations
MyReturnVar.
我想为最终用户提供操作MyReturnVar
变量的最简单方法。在shell脚本中,我执行执行shell中函数的$erl-s myfile main
有没有办法在shell中获取MyReturnVar
另一种方法是直接从shell加载模块
$ erl
1> X = myfile:main().
但是我不太喜欢这个解决方案,我想要一个更“一个命令”的选项(或者我可以在shell脚本中一行执行几个命令)
谢谢当您连续说出几个命令时,听起来好像您想将一个命令的结果导入另一个命令。为此,不使用返回值,返回值只能是int,而是使用stdin和stdout。这意味着您需要将
MyReturnVar
打印到stdout。为此,您有io:format。根据MyReturnVar的值类型,您可以执行以下操作:
-module(myfile).
main() ->
%% do some operations
io:format("~w", [MyReturnVar]),
MyReturnVar.
现在,您应该能够将命令的结果通过管道传输到其他进程。例:
$ erl -s myfile main | cat
您可以(ab)使用.erlang
文件来实现这一点(请参阅erl(1)
手册页)。或者随便闯入
.如果可能,请使用escript
$cat test.escript
#!/usr/local/bin/escript
main([]) ->
MyReturnVar=1,
io:format("~w", [MyReturnVar]),
halt(MyReturnVar).
$escript test.escript
1
$echo $?
1
这将打印出MyReturnVar并返回MyReturnVar,这样您就可以使用pipe或catch$?从shell脚本。不,这个想法不是在终端中显示结果,而是对用户说“只需执行脚本
run.sh
,然后您将在一个带有所需变量的erlang shell中”。我希望能够使用这些变量执行其他erlang操作。我只是想知道,除了X=myfile:main()
操作之外,还有其他方法可以做到这一点。