Getting some errors in escript cli

I’m trying to run this ./elixiralgo_cli.ex --name=Jan

I have everything configured for the escript in elixir file and I have a module something like this

defmodule ElixirAlgo.CLI do
  def main(args) do
    args |> parse_args |> process

  def process([]) do
    IO.puts "No arguments given"

  def process(options) do
    IO.puts "Hello #{options[:name]}"

  defp parse_args(args) do
    {options, _, _} = OptionParser.parse(args,
      switches: [name: :string]

When I’m trying to run this ./elixiralgo_cli.ex --name=Jan

I’m getting this error and I don’t know why

./elixiralgo_cli.ex: line 1: defmodule: command not found
./elixiralgo_cli.ex: line 2: syntax error near unexpected token `('
./elixiralgo_cli.ex: line 2: `  def main(args) do'

Can someone help?

It looks like the bash run it,not the elixir.
Try run it as elixir elixiralgo_cli.ex --name=Jan

This doesn’t return anything

I think this link may help you.
Executables · Elixir School

1 Like

This is not right for this problem.

An escript needs to be built with mix and doesn’t use the .ex extension to run.

On the other hand, invoking it as ./elixiralgo_cli.ex ... tells the shell to run the Elixir file; the standard approach is to include an initlal “shebang” line:

#!/usr/bin/env elixir

When the shell sees this, it knows to call the interpreter specified on this line to handle the rest of the file. Without it, the whole file will be interpreted as shell script which… doesn’t go well and gets you the errors you’re seeing.

1 Like