This module provides an Erlang shell.
The shell is a user interface program
for entering expression sequences. The expressions are
evaluated and a value is returned.
A history mechanism saves previous commands and their
values, which can then be incorporated in later commands.
How many commands and results to save can be determined by the user,
either interactively, by calling
The shell uses a helper process for evaluating commands
to protect the history mechanism from exceptions. By
default the evaluator process is killed when an exception
occurs, but by calling
Variable bindings, and local process dictionary changes that are generated in user expressions are preserved, and the variables can be used in later commands to access their values. The bindings can also be forgotten so the variables can be reused.
The special shell commands all have the syntax of (local) function calls. They are evaluated as normal function calls and many commands can be used in one expression sequence.
If a command (local function call) is not recognized by the
shell, an attempt is first made to find the function in
module
The shell also permits the user to start multiple concurrent jobs. A job can be regarded as a set of processes that can communicate with the shell.
There is some support for reading and printing records in the shell. During compilation record expressions are translated to tuple expressions. In runtime it is not known whether a tuple represents a record, and the record definitions used by the compiler are unavailable at runtime. So, to read the record syntax and print tuples as records when possible, record definitions must be maintained by the shell itself.
The shell commands for reading, defining, forgetting, listing, and
printing records are described below. Notice that each job has its
own set of record definitions. To facilitate matters, record
definitions in modules
-include_lib("kernel/include/file.hrl").
The shell runs in two modes:
Job Control Mode,
Only the currently connected job can 'talk' to the shell.
Prints the current variable bindings.
Removes all variable bindings.
Removes the binding of variable
Prints the history list.
Sets the number of previous commands to keep in the
history list to
Sets the number of results from previous commands to keep in
the history list to
Repeats command
Uses the return value of command
Evaluates
Evaluates
Sets the exception handling of the evaluator process. The
previous exception handling is returned. The default
(
Defines a record in the shell.
Removes all record definitions, then reads record
definitions from the modules
Removes selected record definitions.
Prints all record definitions.
Prints selected record definitions.
Prints a term using the record definitions known to the
shell. All of
Reads record definitions from a module's BEAM file. If
there are no record definitions in the BEAM file, the
source file is located and read instead. Returns the names
of the record definitions read.
Reads record definitions from files. Existing
definitions of any of the record names read are replaced.
Reads record definitions from files but
discards record names not mentioned in
Reads record definitions from files. The compiler
options
The following example is a long dialog with the shell. Commands
starting with
strider 1> erl Erlang (BEAM) emulator version 5.3 [hipe] [threads:0] Eshell V5.3 (abort with ^G) 1> Str = "abcd". "abcd"
Command 1 sets variable
2> L = length(Str). 4
Command 2 sets
3> Descriptor = {L, list_to_atom(Str)}. {4,abcd}
Command 3 builds the tuple
4> L. 4
Command 4 prints the value of variable
5> b(). Descriptor = {4,abcd} L = 4 Str = "abcd" ok
Command 5 evaluates the internal shell command
6> f(L). ok
Command 6 evaluates the internal shell command
7> b(). Descriptor = {4,abcd} Str = "abcd" ok
Command 7 prints the new bindings.
8> f(L). ok
Command 8 has no effect, as
9> {L, _} = Descriptor. {4,abcd}
Command 9 performs a pattern matching operation on
10> L. 4
Command 10 prints the current value of
11> {P, Q, R} = Descriptor. ** exception error: no match of right hand side value {4,abcd}
Command 11 tries to match
12> P. * 1: variable 'P' is unbound 13> Descriptor. {4,abcd}
Commands 12 and 13 show that
14>{P, Q} = Descriptor. {4,abcd} 15> P. 4
Commands 14 and 15 show a correct match where
16> f(). ok
Command 16 clears all bindings.
The next few commands assume that
17> put(aa, hello). undefined 18> get(aa). hello
Commands 17 and 18 set and inspect the value of item
19> Y = test1:demo(1). 11
Command 19 evaluates
20> get(). [{aa,worked}] 21> put(aa, hello). worked 22> Z = test1:demo(2). ** exception error: no match of right hand side value 1 in function test1:demo/1
Commands 21 and 22 change the value of dictionary item
23> Z. * 1: variable 'Z' is unbound 24> get(aa). hello
Commands 23 and 24 show that
25> erase(), put(aa, hello). undefined 26> spawn(test1, demo, [1]). <0.57.0> 27> get(aa). hello
Commands 25, 26, and 27 show the effect of evaluating
28> io:format("hello hello\n"). hello hello ok 29> e(28). hello hello ok 30> v(28). ok
Commands 28, 29 and 30 use the history facilities of the shell. Command 29 re-evaluates command 28. Command 30 uses the value (result) of command 28. In the cases of a pure function (a function with no side effects), the result is the same. For a function with side effects, the result can be different.
The next few commands show some record manipulation. It is
assumed that
31> c(ex). {ok,ex} 32> rr(ex). [rec]
Commands 31 and 32 compile file
33> rl(rec). -record(rec,{a,b = val()}). ok
Command 33 prints the definition of the record named
34> #rec{}. ** exception error: undefined shell command val/0
Command 34 tries to create a
35> #rec{b = 3}. #rec{a = undefined,b = 3}
Command 35 shows the workaround: explicitly assign values to record fields that cannot otherwise be initialized.
36> rp(v(-1)). #rec{a = undefined,b = 3} ok
Command 36 prints the newly created record using record definitions maintained by the shell.
37> rd(rec, {f = orddict:new()}). rec
Command 37 defines a record directly in the shell. The
definition replaces the one read from file
38> #rec{}. #rec{f = []} ok
Command 38 creates a record using the new definition, and prints the result.
39> rd(rec, {c}), A. * 1: variable 'A' is unbound 40> #rec{}. #rec{c = undefined} ok
Command 39 and 40 show that record definitions are updated
as side effects. The evaluation of the command fails, but
the definition of
For the next command, it is assumed that
41> test1:loop(0). Hello Number: 0 Hello Number: 1 Hello Number: 2 Hello Number: 3 User switch command --> i --> c . . . Hello Number: 3374 Hello Number: 3375 Hello Number: 3376 Hello Number: 3377 Hello Number: 3378 ** exception exit: killed
Command 41 evaluates
In this particular case, command
42> E = ets:new(t, []). #Ref<0.1662103692.2407923716.214192>
Command 42 creates an ETS table.
43> ets:insert({d,1,2}). ** exception error: undefined function ets:insert/1
Command 43 tries to insert a tuple into the ETS table, but the first argument (the table) is missing. The exception kills the evaluator process.
44> ets:insert(E, {d,1,2}). ** exception error: argument is of wrong type in function ets:insert/2 called as ets:insert(16,{d,1,2})
Command 44 corrects the mistake, but the ETS table has been destroyed as it was owned by the killed evaluator process.
45> f(E). ok 46> catch_exception(true). false
Command 46 sets the exception handling of the evaluator process
to
47> E = ets:new(t, []). #Ref<0.1662103692.2407923716.214197> 48> ets:insert({d,1,2}). * exception error: undefined function ets:insert/1
Command 48 makes the same mistake as in command 43, but this time the evaluator process lives on. The single star at the beginning of the printout signals that the exception has been caught.
49> ets:insert(E, {d,1,2}). true
Command 49 successfully inserts the tuple into the ETS table.
50> ets:insert(#Ref<0.1662103692.2407923716.214197>, {e,3,4}). true
Command 50 inserts another tuple into the ETS table. This time
the first argument is the table identifier itself. The shell can
parse commands with pids (
51> halt(). strider 2>
Command 51 exits the Erlang runtime system.
When the shell starts, it starts a single evaluator
process. This process, together with any local processes that
it spawns, is referred to as a
All jobs that do not use standard I/O run in the normal way.
The shell escape key
--> ? c [nn] - connect to job i [nn] - interrupt job k [nn] - kill job j - list all jobs s [shell] - start local shell r [node [shell]] - start remote shell q - quit erlang ? | h - this message
The
Connects to job number
Stops the current evaluator process for job number
Kills job number
Lists all jobs. A list of all known jobs is printed. The current job name is prefixed with '*'.
Starts a new job. This is assigned the new index
Starts a new job. This is assigned the new index
Starts a remote job on
Quits Erlang. Notice that this option is disabled if
Erlang is started with the ignore break,
Displays the help message above.
The behavior of shell escape can be changed by the STDLIB
application variable
If you want an Erlang node to have a remote job active from the start
(rather than the default local job), start Erlang with flag
The shell can be started in a
restricted mode. In this mode, the shell evaluates a function call
only if allowed. This feature makes it possible to, for example,
prevent a user from accidentally calling a function from the
prompt that could harm a running system (useful in combination
with system flag
When the restricted shell evaluates an expression and
encounters a function call or an operator application,
it calls a callback function (with
information about the function call in question). This callback
function returns
This is used to determine if the call to the local function
This is used to determine if the call to non-local function
These callback functions are called from local and
non-local evaluation function handlers, described in the
Argument
There are two ways to start a restricted shell session:
Use STDLIB application variable
From a normal shell session, call function
Notes:
When restricted shell mode is activated or deactivated, new jobs started on the node run in restricted or normal mode, respectively.
If restricted mode has been enabled on a particular node, remote shells connecting to this node also run in restricted mode.
The callback functions cannot be used to allow or disallow execution of functions called from compiled code (only functions called from expressions entered at the shell prompt).
Errors when loading the callback module is handled in different ways depending on how the restricted shell is activated:
If the restricted shell is activated by setting the STDLIB
variable during emulator startup, and the callback module cannot be
loaded, a default restricted shell allowing only the commands
If the restricted shell is activated using
The default shell prompt function displays the name of the node
(if the node can be part of a distributed system) and the
current command number. The user can customize the prompt
function by calling
A customized prompt function is stated as a tuple
Sets the exception handling of the evaluator process. The
previous exception handling is returned. The default
(
Sets the number of previous commands to keep in the
history list to
Sets the shell prompt function to
Sets the number of results from previous commands to keep in
the history list to
Exits a normal shell and starts a restricted shell.
If the callback module cannot be loaded, an error tuple is
returned. The
Exits a restricted shell and starts a normal shell. The function is meant to be called from the shell.
Sets pretty printing of lists to
The flag can also be set by the STDLIB application variable