1
Fork 0

try out os::exec and pipe

This commit is contained in:
Jan-Erik Rediger 2024-05-26 16:14:50 +02:00
parent 5ac69ba7e2
commit 43a6d458a5
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,35 @@
use fmt;
use io;
use os::exec;
use os;
use strings;
export fn main() void = {
let cmd = exec::cmd("./run.sh")!;
let stdout_pipe = exec::pipe();
exec::addfile(&cmd, os::stdout_file, stdout_pipe.1);
let stderr_pipe = exec::pipe();
exec::addfile(&cmd, os::stderr_file, stderr_pipe.1);
let proc = exec::start(&cmd)!;
io::close(stdout_pipe.1)!;
io::close(stderr_pipe.1)!;
let stdout_data = io::drain(stdout_pipe.0)!;
io::close(stdout_pipe.0)!;
let stderr_data = io::drain(stderr_pipe.0)!;
io::close(stderr_pipe.0)!;
let status = exec::wait(&proc)!;
let s = exec::exit(&status) as int;
fmt::printfln("status: {}", s)!;
let out = strings::fromutf8(stdout_data) as str;
fmt::printfln("stdout:\n{}", out)!;
let out = strings::fromutf8(stderr_data) as str;
fmt::printfln("stderr:\n{}", out)!;
};

9
backend/run.sh Executable file
View file

@ -0,0 +1,9 @@
#!/bin/bash
echo "first line"
echo "error 1" >&2
echo "second line"
echo "error 2" >&2
echo "third line"
exit 17