Files and processes

Write and read a temporary file, then run a process as an argument array without a shell.

temp_file chooses a temporary path, and remove_file cleans it up after the example. system receives a list where the first item is the program and the remaining items are its arguments; it returns a result map, including captured standard output. No shell parses this list.

Script: files-processes.lms

path = temp_file();
write_file(path, "first line\nsecond line\n");
lines = read_lines(path);
println(f"lines={lines.len()} last={lines[1]}");
result = system(["node", "-e", "process.stdout.write('process-ok')"]);
println(f"process={result.stdout}");
remove_file(path);

Run it

lmscript examples/files-processes.lms

Output

lines=2 last=second line
process=process-ok

Previous: Strings and data · Next: Errors and require

Try it: download examples/files-processes.lms, save it under examples/ in a writable working directory, then run the command above from that directory.