%cat fact
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
main([String]) ->
try
N = list_to_integer(String),
F = fac(N),
io:format("factorial ~w = ~w\n", [N,F])
catch
_:_ ->
usage()
end;
main(_) ->
usage().
usage() ->
io:format("usage: factorial integer\n"),
halt(1).
fac(0) -> 1;
fac(N) -> N * fac(N-1).
%./fact "5"
escript: no such file or directory: './fact'
%whereis escript
escript: /usr/bin/escript
%pacman -Qi erlang
name : erlang
version : R14B04-1
Why doesnot escript run "fact" ?
On my Archlinux box, escript still doesnot work !
%cat hello.erl
main(_) -> io:fwrite("~p~n", "hello,world!").
%escript hello.erl
escript: no such file or directory: 'hello.erl'
%whereis escript
escript: /usr/bin/escript
%ls -l /usr/bin/escript
lrwxrwxrwx 1 root root 25 12月 18 17:37 /usr/bin/escript -> ../lib/erlang/bin/escript*
%/usr/lib/erlang/bin/escript hello.erl
escript: no such file or directory: 'hello.erl'
%strace -f -F -o aaa.txt /usr/lib/erlang/bin/escript hello.erl
escript: no such file or directory: 'hello.erl
%cat aaa.txt
execve("/usr/lib/erlang/bin/escript", ["/usr/lib/erlang/bin/escript", "hello.erl"], [/* 40 vars */]) = 0
...
open("hello.erl", O_RDONLY|O_LARGEFILE) = 3
...
execve("/usr/lib/erlang/bin/erl", ["/usr/lib/erlang/bin/erl", "+B", "-boot", "start_clean", "-noshell", "-run", "escript", "start", "-extra", "hello.erl"], [/* 40 vars */]) = 0.
...
stat64("hello.erl", 0xb5a44d90) = -1 ENOENT (No such file or directory)
open("hello.erl", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
......
Why does it use "stat64" instead of "stat" ? I am using 32 bits system.
%uname -a
Linux myarch 3.1.5-1-ARCH #1 SMP PREEMPT Sun Dec 11 06:26:14 UTC 2011 i686 AMD Athlon(tm) 64 X2 Dual Core Processor 3600+ AuthenticAMD GNU/Linux
%erl -version
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 5.8.5
Sincerely!
%ls
fact*
%escript fact "5"
escript: no such file or directory: 'fact'
%escript fact 5
escript: no such file or directory: 'fact
%ls -l /usr/bin/escript
lrwxrwxrwx 1 root root 25 10月 15 03:24 /usr/bin/escript -> ../lib/erlang/bin/escript*
Strange problem ?
** NOTE * This answer is customized for Windows Users, but can be understood and useful to other opertaing system users
Is escript in the $PATH environment variable ? usually its hidden in ERTS_PATH/bin where ERTS_PATH is in C:\Program Files (x86)\erl5.8.4\erts-5.8.4\ in Windows 7. look for the equivalent on Linux or Unix or MAC for erts. add this path (C:\Program Files (x86)\erl5.8.4\erts-5.8.4\bin) to the $PATH. escript should be able to work anywhere
The reason is VERY simple. Because the following line is in ~/.erlang:
file:set_cwd("/media/D/www/qachina/db/doc/erlang")
so that escript will change the current directory once executed. The escript works great after removing that line.
Best Regards!
The same script I copied and it is working.
Make sure after you have written the above script, you are running the script as follows
escript <scriptName> <argument>
eg:- escript fact 5
For detailed information:
http://www.erlang.org/doc/man/escript.html
activate the erlang, lets say erlang is in root:
. /root/erlang/r15b01/activate
Related
I want to build an Erlang hello world example using rules_erlang on Ubuntu 22.04.
My setup looks like this:
BUILD.bazel
load("#rules_erlang//:erlang_app.bzl", "erlang_app", "test_erlang_app")
load("#rules_erlang//:xref.bzl", "xref")
load("#rules_erlang//:dialyze.bzl", "dialyze", "plt")
load("#rules_erlang//:ct.bzl", "ct_suite", "assert_suites")
APP_NAME = "hello_world"
APP_VERSION = "0.1.0"
erlang_app(
app_name = APP_NAME,
app_version = APP_VERSION,
)
src/hello_world.erl
-module(hello_world).
-compile(export_all).
hello() ->
io:format("hello world~n").
WORKSPACE.bazel
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_skylib",
sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
],
)
load("#bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
http_archive(
name = "rules_erlang",
sha256 = "5e59800ecc786d5375951028c959c6e6275c94eff2a52f5d53ccb1ad8b2ea20a",
strip_prefix = "rules_erlang-3.8.4",
urls = ["https://github.com/rabbitmq/rules_erlang/archive/refs/tags/3.8.4.zip"],
)
load(
"#rules_erlang//:rules_erlang.bzl",
"erlang_config",
"rules_erlang_dependencies",
)
erlang_config()
rules_erlang_dependencies()
load("#erlang_config//:defaults.bzl", "register_defaults")
register_defaults()
The code can also found here.
When I execute bazel build //... I get
ERROR:
/home/vertexwahn/.cache/bazel/_bazel_vertexwahn/b5f945f94177a8ffa6ac0f7108dfc1cd/external/erlang_config/external/BUILD.bazel:12:16:
Validating otp at /usr failed: (Exit 1): bash failed: error executing
command /bin/bash -c ... (remaining 1 argument skipped)
Use --sandbox_debug to see verbose messages from the sandbox and
retain the sandbox build root for debugging Erlang version mismatch
(Expected UNKNOWN, found 24.2.1)
Any hints to get this working are welcome!
To run it, you can declare a shell rule in your BUILD.bazel, for instance:
load("#rules_erlang//:shell.bzl", "shell")
shell(
name = "repl",
deps = [":erlang_app"],
extra_erl_args = ["-eval", "'hello_world:hello()'"],
)
and then you could bazel run :repl.
Or, you could use an escript rule if you change hello_world.erl so that it exports main/1 instead of hello:
load("#rules_erlang//:escript.bzl", "escript_archive")
escript_archive(
name = "hello_world",
app = ":erlang_app",
)
hello_world.erl:
-module(hello_world).
-export([main/1]).
main(_) ->
io:format("hello world~n").
and run with bazel run :hello_world
bazel build //... --sandbox_debug
gave me
compile: warnings being treated as errors
hello_world.erl:2:2: export_all flag enabled - all functions will be exported
With -export([hello/0]). instead of -compile(export_all). it works
-module(hello_world).
-export([hello/0]).
hello() ->
io:format("hello world~n").
❯ bazel build //...
INFO: Analyzed 3 targets (0 packages loaded, 0 targets configured).
INFO: Found 3 targets...
INFO: Elapsed time: 0,326s, Critical Path: 0,25s
INFO: 2 processes: 1 internal, 1 darwin-sandbox.
INFO: Build completed successfully, 2 total actions
~ ll /usr/local/bin| grep vrl
-rwxrwxrwx 1 root root 31K 9月 29 09:38 vrlsubmit
➜ ~ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
➜ ~ vrlsubmit
zsh: command not found: vrlsubmit
Finally somebody helped me:
Root cause is:
I am trying to exec a 32bit command in 64bit system
Debug:
> file submit # < submit stands for the command >
submit: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.2.5, not stripped
> ls /lib | grep ld
compat-ld
gold-ld
Based on above "file submit", it tells "interpreter /lib/ld-linux.so.2" needed. But there are no /lib/ld-linux.so.2 file at all!!!
Solution:
sudo apt-get install lib32z1 -y
submit
Error: Missing one or more required arguments.
submit
Finished to resolve this issue!
I am trying to use in Erlang module a beam-file compiled from Elixir source. It raises error when I run Erlang node but I can use the code directly from Elixir.
Elixir module:
defmodule Recursion do
def print_multiple_times(msg, n) when n <= 1 do
IO.puts msg
end
def print_multiple_times(msg, n) do
IO.puts msg
print_multiple_times(msg, n - 1)
end
end
Erlang module:
-module(use_recur).
-define(elixir__recursion, 'Elixir.Recursion').
-export([hey/0]).
hey() ->
?elixir__recursion:print_multiple_times("Hello!", 3).
Compile both:
$ rm -f *.beam
$ elixirc recursion.ex
$ erlc use_recur.erl
Run Erlang:
$ erl -run use_recur hey -run init stop -noshell {"init terminating in do_boot",{undef,[{'Elixir.IO',puts,["Hello!"],[]},{'Elixir.Recursion',print_multiple_times,2,[{file,"recursion.ex"},{line,7}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ({undef,[{Elixir.IO,puts,Hello!,[]},{Elixir.Recursion,print_multiple_times,2,[{},{}]},{init,start_em,1,[]},{init,do_boot,3,[]}]})
Crash dump is being written to: erl_crash.dump...done
Elixir script:
Recursion.print_multiple_times "Hello!", 3
Runs successfully:
$ elixir elx_recur.exs
Hello!
Hello!
Hello!
Why does it happen? I'd say Erlang's output should be the same.
The error means that Erlang could not find a module named 'Elixir.IO'. This module is part of core Elixir. You'll need to add the ebin folder of your Elixir installation to Erlang's load path using -pa (or other similar flags like -pz) to make Erlang load Elixir's core libraries, as that folder contains the compiled .beam files of Elixir core, including Elixir.IO.beam.
erl -pa /path/to/elixir/ebin ...
rebar is in the current directory.
$ls -l rebar
--rwxr-xr-x 1 *** wheel 90778 8 6 23:05 rebar*
$./rebar
escript: no such file or directory: './rebar'
$whereis escript
escript: /usr/local/bin/escript
I am using FreeBSD 9.0.
thanks!
You should change in the beginning of rebar file:
#!/usr/bin/env escript
to:
#!/usr/local/bin/env escript
or just:
#!/usr/local/bin/escript
I have an Emakefile that looks like:
%% --
%%
%% --
{'/Users/user/projects/custom_test/trunk/*',
[debug_info,
{outdir, "/Users/user/projects/custom_test/trunk/ebin"},
{i, "/Users/user/projects/custom_test/trunk/include/."}
]
}.
What is an explanation in layman's terms for what each item does in the list?
How do I run the emakefile so that I am able to compile it?
After compilation, how do I run that generated BEAM file?
1/ {"source files globbed", Options}
Here the options are :
debug_info add debug info for the debugger
{outdir, "/Users/user/projects/custom_test/trunk/ebin"} where should the output be written (the .beam files)
{i, "/Users/user/projects/custom_test/trunk/include/."} where to find the .hrl header files.
2/ erl -make
3/ erl -pa /Users/user/projects/custom_test/trunk/ebin starts a shell.
Find the module serving as an entry point in your application and call the functions :
module:start().
You can also run the code non interactively :
erl -noinput -noshell -pa /Users/user/projects/custom_test/trunk/ebin -s module start
For the Emakefile synax visit the man page
In the directory where the Emakefile is run erl -make to compile using the Emakefile
Simplest way to run would be to simply start an erlang shell in the same directory as the beam files with the command erl. Then run the code with module_name:function_name(). (including the dot).