Question

I need to pass two arguments to my Erlang code. it is working fine in the Erlang shell.

2> crop:fall_velocity(x,23).
  21.23205124334434

but how should i run the Erlang code without the Erlang shell. like normal python,c programs. ./program_name (not passing $1 $2 arguments).

I was trying this

erl -noshell -s crop fall_velocity(x,20) -s init stop

But it is giving unexpected token error.

Was it helpful?

Solution

As documentation states, the -s passes all parameters supplied as just one list of atoms and -run does the same but as a list of strings. If you want to call arbitrary function with arbitrary parameter count and types you should use -eval:

$ erl -noshell -eval 'io:format("test\n",[]),init:stop()'
test
$

OTHER TIPS

You can use escript to run Erlang scripts from the command line. In that script you should create a main function which takes an array of arguments as a string.

#!/usr/bin/env escript

main(Args) ->
  io:format("Printing arguments:~n"),
  lists:foreach(fun(Arg) -> io:format("Got argument: ~p~n", [Arg]) end,Args).

Output:

./escripter.erl hi what is your name 5 6 7 9
Printing arguments:
Got argument: "hi"
Got argument: "what"
Got argument: "is"
Got argument: "your"
Got argument: "name"
Got argument: "5"
Got argument: "6"
Got argument: "7"
Got argument: "9"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top