gpt4 book ai didi

bash - 从 tclsh 调用 bash 内置函数

转载 作者:行者123 更新时间:2023-11-29 09:28:54 26 4
gpt4 key购买 nike

我有一个 tclsh 脚本,我需要在其中在后台执行某些命令。我可以使用 exec 命令从 tcl 实现这一点:exec myprog &

但是我如何等待 myprog 从 tcl 完成。命令 wait 不是一个独立的实用程序,因此我可以将它与 exec 一起使用。wait 命令是一个 shell 内置命令。请让我知道如何在 tclsh 脚本中等待后台进程。

PS:我在我的脚本中使用了#!/usr/bin/env tclsh shebang。

最佳答案

如果你想在 Tcl 的后台执行一个命令,你可以按照以下方式去做:

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

在命令前使用 open| 将允许您“打开”命令的管道。使用 fconfigure 将其设置为非阻塞将允许您从中读取而无需锁定脚本中的任何其他进程。只要有数据要读取(因此 readable 标志),fileevent 就会调用指定的回调过程(在本例中为 cb)。 vwait 将确保脚本在写入指定变量之前不会继续,因此 $command 将在后台执行,允许 Tk 接口(interface)保持响应等到您想继续。

关于bash - 从 tclsh 调用 bash 内置函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8727876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com