Cecil Westerhof <
Ce...@decebal.nl> wrote:
> I wanted output from a command seen at once. With exec I only get to
> see it when the command is finished. For this I created the following
> proc:
> proc execCmd {cmd {displayOutput True} {returnOutput False}} {
> set cmdPipe [open |${cmd} RDONLY]
> set output {}
>
> while {-1 != [gets ${cmdPipe} line]} {
> if {${displayOutput}} {
> puts ${line}
> }
> if {${returnOutput}} {
> lappend output ${line}
> }
> }
> close ${cmdPipe}
> return ${output}
> }
>
> Default it displays everything as soon as it becomes available and
> returns nothing, but both can be overridden.
>
> Is this OK, or should I do it in another way?
Two comments:
1) you are reading input line by line, but when you choose to return
the output, you are returning a list of lines, not a string. So the
return value from your proc is not identical to the return value from a
pure exec. If you want the same return value, change your return to
be: return [join $output \n]\n
The extra \n on the end is to account for the very last \n that was
dropped by the very last gets in the loop.
2) This will work just fine, as long as you do not want to use it as
part of an event loop (either in a GUI or in a CLI that itself makes
use of the event loop). When in an event loop this variant will pause
the event loop until the exec'ed process exits. This may or may not be
what you want, depending on what the code using this proc would be
trying to do when calling it.