capture

Returns a command’s output as a string

Format

capture [ -out | -err | -both ] [ -f filename ] command

Arguments

-out

Captures only output sent to stdout.

-err

Captures only output sent to stderr.

-both

Captures output sent to both stdout and stderr. This is the default.

-f filename

Sends the captured output to filename. The file must be a writable Tcl file descriptor. Usually the Tcl file descrciptor name is obtained with open filename w.

command

The CLI command (or commands) whose output is being captured. If you specify more than one command, you must enclose them within braces ({ }).

Description

The capture command executes command, capturing in a string all output that would normally go to the console. After command completes, it returns the string. This command is analogous to the UNIX shell’s back-tick feature (`command`). The capture command obtains the printed output of any CLI command so that you can assign it to a variable or otherwise manipulate it.

Examples

set save_stat [ capture st ]

Saves the current process status to a Tcl variable.

set arg [ capture p argc]

Saves the printed value of argc into a Tcl variable.

set vbl [ capture {foreach i {1 2 3 4} {p int2_array\[$i\]}} ]

Saves the printed output of four array elements into a Tcl variable. Here is sample output:

int2_array(1) = -8 (0xfff8)

int2_array(2) = -6 (0xfffa)

int2_array(3) = -4 (0xfffc)

int2_array(4) = -2 (0xfffe)

Because the capture command records all information sent to it by the commands in the foreach loop, you do not have to use a dlist command.

exec cat << [ capture help commands ] > cli_help.txt

Writes the help text for all CLI commands to the cli_help.txt file.

set ofile [open cli_help.txt w]

capture -f $ofile help commands

close $ofile

Also writes the help text for all CLI commands to the cli_help.txt file. This set of commands is more efficient than the previous command because the captured data is not buffered.

Related Topics

drun

drerun