Tcl 如何使变量唯一

Tcl 如何使变量唯一,tcl,Tcl,如何使变量在TCL中唯一 例如: exec echo $msgBody - /tmp/Alert_Notify_Work.$$ exec cat /home/hci/Alert.txt -- /tmp/Alert_Notify_Work.$$ 这是行不通的;我正在尝试使变量Alert\u Notify\u Work唯一。$$不是有效的Tcl语法,Tcl将在shell看到它之前解析该行。但是有一个Tcl命令来检索pid:pid。我通常依赖于当前时间和pid的唯一性 我假设msgBody是一个Tc

如何使变量在TCL中唯一

例如:

exec echo $msgBody - /tmp/Alert_Notify_Work.$$
exec cat /home/hci/Alert.txt -- /tmp/Alert_Notify_Work.$$

这是行不通的;我正在尝试使变量
Alert\u Notify\u Work
唯一。

$$
不是有效的Tcl语法,Tcl将在shell看到它之前解析该行。但是有一个Tcl命令来检索pid:
pid
。我通常依赖于当前时间和pid的唯一性

我假设
msgBody
是一个Tcl变量,命令中的
-
-
应该分别是
>

选择1

set filename /tmp/Alert_Notify_Work.[clock seconds].[pid]
exec echo $msgBody > $filename
exec cat /home/hci/Alert.txt >> $filename
或者,Tcl只需再增加几行:

set f_out [open /tmp/Alert_Notify_Work.[clock seconds].[pid] w]
puts $f_out $msgBody
set f_in  [open /home/hci/Alert.txt r]
fcopy $f_in $f_out
close $f_in
close $f_out

$
不是有效的Tcl语法,Tcl将在shell看到该行之前解析该行。但是有一个Tcl命令来检索pid:
pid
。我通常依赖于当前时间和pid的唯一性

我假设
msgBody
是一个Tcl变量,命令中的
-
-
应该分别是
>

选择1

set filename /tmp/Alert_Notify_Work.[clock seconds].[pid]
exec echo $msgBody > $filename
exec cat /home/hci/Alert.txt >> $filename
或者,Tcl只需再增加几行:

set f_out [open /tmp/Alert_Notify_Work.[clock seconds].[pid] w]
puts $f_out $msgBody
set f_in  [open /home/hci/Alert.txt r]
fcopy $f_in $f_out
close $f_in
close $f_out

为此,最好使用预先存在的库。Tcllib有一个fileutil包,它实现tempfiles:

set filename [fileutil::tempfile Alert_Notify_Work.]

为此,最好使用预先存在的库。Tcllib有一个fileutil包,它实现tempfiles:

set filename [fileutil::tempfile Alert_Notify_Work.]

+1:我会用
clock clicks
来代替我自己(或8.6中的
file tempfile
),但你的建议是可行的+1:我会用
clock clicks
来代替我自己(或
file tempfile
来代替8.6中的
file tempfile
),但你的建议是可行的。