Reputation: 5411
I have a script file in erlang that start some modules. In the erlang shell, I would like to use the returned objects from the start function.
I have my file :
-module(myfile).
main() ->
%% do some operations
MyReturnVar.
I want to have the easiest way for the final user to manipulate the MyReturnVar
variable. In the shell script, I do $ erl -s myfile main
which executes the function in the shell.
Is there a way to get the MyReturnVar in the shell ?
Another way is to load the module directly from the shell
$ erl
1> X = myfile:main().
but I don't like so much this solution, I'd like a more "one command" option (or several in a row I can do in a shell script).
Thank you
Upvotes: 0
Views: 476
Reputation: 27748
Use escript if possible.
$cat test.escript
#!/usr/local/bin/escript
main([]) ->
MyReturnVar=1,
io:format("~w", [MyReturnVar]),
halt(MyReturnVar).
$escript test.escript
1
$echo $?
1
This will printout MyReturnVar and return MyReturnVar, so that you can use with pipe or just catch $? from shell script.
Upvotes: 0
Reputation: 61
You could (ab)use the .erlang
file to achieve this (see the erl(1)
man page). Or hack around in erlang-history
.
Upvotes: 0
Reputation: 4446
When you say several in a row, it sounds like you want to pipe the result of one command into another command. For that you don't use the return value, which can only be an int, but you use stdin and stdout. Meaning what you want is to print MyReturnVar
to stdout. For that you have io:format. Depending on what type of value MyReturnVar
is you would do something like this:
-module(myfile).
main() ->
%% do some operations
io:format("~w", [MyReturnVar]),
MyReturnVar.
Now you should be able to pipe the result for your command to other processes. Ex:
$ erl -s myfile main | cat
Upvotes: 1