Eve Commands
call statement, or start a program without a shell with os.run!.
System variables
System variables start with the prefix $. Every environment variable of the operating system is visible as $NAME. There is no export statement: a program started with os.run! receives extra variables through its env argument. When you work with files and folders, the system security is effective: you can't access a file over which you have no user rights.
These system variables are available by default in Eve:
$OS_PWD: the current working folder, read-only;$HOME: the home directory of the user;$EVE_DIR: the folder of the Eve runtime (the folder ofeve.exe);$EVE_HOME: the folder of the project, the folder of the script that runs;$EVE_ASP: the folder of the aspects of the project (asp);$EVE_LIB: thelibfolder of the project;$EVE_OUT: the output folder of the log files (outby default);$NAME: any environment variable, for example$PATH.
All the system variables are listed in System Variables.
Shell command
The call keyword sends a command to the operating system and executes a native kernel command.
Shell syntax
call [input +>] command [+> output]
command ::= "os_command -flags {value} {value} ..."
os_command::= {ls, cd, rm, chmod, ...}
Note: The shell commands are string literals or variables. Binding Eve variables to OS command is using string interpolation. That means you can use placeholders such as {name} in the command string; a literal brace is \{ or \}.
Using call
Call is useful to execute an OS shell script (.sh) or a OS commands, usually this will produce an output to console. The output can be redirected to a file or piped to another command.
capture output
You can capture the output as a list of strings using operator +>. In next example we capture a list of files
# listing files in folder "test"
primary aspect shell_output is
set files: ()String;
process main is
call "cd {$HOME}/test";
call "ls -l *.dat" +> files;
loop
new file: String;
for file in files do
print file;
done;
return;
end shell_output;
accept input
# execute a shell script
primary aspect shell_input is
set files: ()String;
process main is
call "cd {$HOME}/test";
call ("hello", "world") +> "./shell_test.sh";
return;
end shell_input;
The input must be accepted by the script using "read".
#!/bin/bash
while read line
do
echo $line
done < /dev/stdin
Programs without a shell: os.run!
The module os starts a program from an argument list, without a shell: there is no quoting problem and no injection. call stays for a whole command line. The name ends with ! because the result depends on the machine, like a file read.
new r := os.run!("git", ("status", "--short"), "work");
print "exit {r.status}";
print r.out;
os.run!(command, args, cwd) starts command with the arguments of the list, each a String, in the folder cwd ("" means here), waits for it and gives a ProcessResult with the fields status, out and err. The standard input of the child is closed. Two more arguments are optional: timeout_ms kills the child after that many milliseconds (the status is then 124), and env is a map of String to String added to the environment of the child. A program that can not be started gives status 127 and the reason in err; it is not an error of the language, because a script often wants to test it.
Use it whenever the arguments are built from data, as in an ETL job that calls a compressor or a loader. The database administration commands (db list, db sql, db describe, db migrate, db validate) are machine commands; see Databases.
Listing a folder with fs
Do not read a folder through a shell command. The module fs does it directly: fs.list! gives the names in a folder, and fs.read! reads a file as text. There is no Folder class in Eve 0.0.8.
# listing the files of a folder
primary aspect folder_test is
process main is
new folder := $HOME / "test";
new files := fs.list!(folder);
for file in files do
print file;
done;
return;
end folder_test;
fs.list! gives a list of String names; the operator / joins two parts of a path. See Files and ETL for the other functions of fs.
Tools for scripts
Two commands work on the source of a project and run nothing. Both take files and folders (a folder gives its .eve files and those of its sub-folders), report a syntax error as file:line:col: error: text and go on with the next file.
eve fmt [--check] projectwrites the layout of the scripts: LF line endings, no white space at the end of a line, at most one blank line in a row, one line ending at the end of the file. The lines of a"""text are data and stay as they are. It changes nothing else (the indentation is checked by the compiler), and formatting a formatted file changes nothing. With--checkit writes nothing and ends with the exit code 1 when a file would change, so a build can refuse an unformatted commit;eve lint projectreports the names that are declared and never used: variables, parameters of functions and procedures, loop variables and imports. A name that starts with_is meant to be unused. It ends with the exit code 1 when it reports something.
$ eve lint orders.eve
orders.eve:5:9: warning: variable 'scratch' is never used
orders.eve:12:9: warning: loop variable 'i' is never used
lint: 1 file(s), 2 warning(s)
$ eve fmt --check .
orders.eve: not formatted
fmt: 3 file(s), 1 not formatted
Read next: EveDB