Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invoking bash builtins from tclsh

Tags:

bash

built-in

tcl

I have a tclsh script in which I needed to execute certain command in background. I could achieve this from tcl using exec command: exec myprog &.

But how can I wait for myprog to complete from tcl. The command wait is not a standalone utility so I can use it with exec. The wait command is a shell builtin. Please let me know how can I wait for the background process in tclsh script.

PS: I am using #!/usr/bin/env tclsh shebang in my script.

like image 779
Mallik Avatar asked Nov 15 '25 15:11

Mallik


1 Answers

If you want to execute a command in the background in Tcl, you could go for something along the line of:

proc cb { fd } {
        gets $fd buf
        append ::output $buf
        if {[eof $fd]} {
            close $fd
            set ::finished 1
        }
}

set command "<command to execute>"
set ::output ""
set ::finished 0
set fd [open "|$command" r]
fconfigure $fd -blocking no
fileevent $fd readable "cb $fd"
vwait ::finished
puts $::output

The use of open with a | before the command will allow you to "open" a pipe to the command. Using fconfigure to set it non-blocking will allow you to read from it without locking any other process in the script. fileevent will call the specified callback proc (in this case cb) whenever there's data to be read (thus the readable flag). vwait will make sure the script does not proceed until the specified variable is written to, thus the $command will be executed in the background, allowing say a Tk interface to remain responsive and waiting until you want to continue.

like image 144
zrvan Avatar answered Nov 17 '25 09:11

zrvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!