how do i make a variable unique
How do I make a variable unique in TCL?
Example:
exec echo $msgBody - /tmp/Alert_Notify_Work.$$
exec cat /home/hci/Alert.txt -- /tmp/Alert_Notify_Work.$$
开发者_开发技巧
This does not work; I am trying to make the variable Alert_Notify_Work
unique.
It's best to use a pre-existing library for this. Tcllib has a fileutil package that implements tempfiles:
set filename [fileutil::tempfile Alert_Notify_Work.]
$$
is not valid Tcl syntax, and Tcl will parse that line before the shell sees it. But there is a Tcl command to retrieve the pid: pid
. I usually rely on the current time and the pid for uniqueness.
I assume msgBody
is a Tcl variable, and the -
and --
in your commands should be >
and >>
respectively.
option 1
set filename /tmp/Alert_Notify_Work.[clock seconds].[pid]
exec echo $msgBody > $filename
exec cat /home/hci/Alert.txt >> $filename
or, Tcl only with just a few more lines:
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
精彩评论