Skip to content

Core\Process\Result::stdout

$result->stdout(): bytes

Returns everything a program wrote to its standard output. The standard output is where a program prints its normal results. Error messages go to the standard error instead, and stderr returns those.

The result is tainted bytes, not string. A program can write any data, for example an image or a compressed file, and that data is often not valid text. When you know the output is text, use as string to convert it. That conversion throws an error if the output is not valid UTF-8.

The output is tainted because another program wrote it, and it may contain text from outside your program. Write tainted bytes or tainted string on the variable that keeps it. A response body, a log and a file all take tainted text.

The program has already ended when you have a Core\Process\Result, so the whole output is there. stdout never throws an error, and it returns the same output every time you call it.

The examples below read the output as text, read output that is not text, and use a system tool to format a date.

Return value

Returns bytes. The output, as tainted bytes. A program can write data that is not valid text, so the result is not a string. Use as string to convert it to text. That throws an error if the output is not valid UTF-8.

Changelog

VersionChange
0.0.6The output of a child process is tainted bytes. The version text of a database server and the name an LDAP server returns are tainted string. Another program or a server writes this text, so it may contain input from outside.

Examples

Reading the output as text

<?nvs
// Shows how to read what a program printed, as text.
// requires: unix
var $result = Core\Process::run('/bin/echo', ['Hello from another program']);
// `stdout()` returns `bytes`. `as string` converts them to text.
tainted string $text = $result->stdout() as string;
// `echo` ends its output with a new line. `Core\Str::trim` removes it.
// This prints `[Hello from another program]`.
echo '[', Core\Str::trim($text), "]\n";
Output
[Hello from another program]

Output that is not text

<?nvs
// Shows that the output of a program can be data that is not text.
// requires: unix
// `printf` writes three bytes here. The last one, `\377`, is not valid UTF-8.
var $result = Core\Process::run('/usr/bin/printf', ["AB\\377"]);
tainted bytes $data = $result->stdout();
// `bytes` can contain any data. This prints `3`.
echo Core\Bytes::length($data), "\n";
// The data is not valid text, so `as string` throws an error.
try {
tainted string $text = $data as string;
echo $text, "\n";
} catch (RuntimeError $notText) {
echo 'not text: ', $notText->message, "\n";
}
Output
3
not text: cannot convert `bytes` to `string`: not well-formed UTF-8 at byte 2

A number from a tool

<?nvs
// Shows a small method that gets one value from a system tool and returns it as a whole number.
// requires: unix
class Days
{
// `expr` calculates with whole numbers. A day has 86400 seconds, so this
// returns how many whole days passed between 1970-01-01 and the timestamp.
public static function since1970(int $timestamp): int
{
var $result = Core\Process::run('/bin/expr', [$timestamp as string, '/', '86400']);
if ($result->exitCode() != 0) {
throw new RuntimeError('expr failed with exit code ' . $result->exitCode());
}
// The output ends with a new line. `Core\Str::trim` removes it.
// `as int` converts the text to a whole number.
return Core\Str::trim($result->stdout() as string) as int;
}
}
// This prints `19675` and then `1`.
echo Days::since1970(1700000000), "\n";
echo Days::since1970(86400), "\n";
Output
19675
1