1

In slsh I know that I have to !-prefix a command to execute it:

!ls 

How can I capture its output in a variable?

1

I can't find anything in the docs about a way to do this directly, but you can create functions to do that yourself relatively easily with the help of popen.

If you want to grab all the output into a list of strings, you could do this:

define grab_output (command)
{
    variable fp, lines;

    fp = popen (command, "r");
    if (fp == NULL)
        throw OpenError, "Failed to popen ($command)";
    lines = fgetslines(fp);
    () = fclose (fp);
    return lines;
}

If you want to process each line as they're coming, then using something like this would probably be better:

define display_output (command)
{
    variable fp, line;

    fp = popen (command, "r");
    if (fp == NULL)
        throw OpenError, "Failed to popen ($command)";
    foreach line (fp) using ("line")
    {
        printf("% 5d - %s", strlen(line), line);
    }
    () = fclose (fp);
}

Usage:

variable line;
variable lines = grab_output("/usr/bin/cal");
foreach line (lines)
{
    printf("stdout: %s", line); 
}

display_output("cat t.sl");

Warning: this was simply pieced together from examples in the S-Lang guide, I don't actually know the language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.