Mike Brown
Mike Brown

Reputation: 11

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.

Upvotes: 1

Views: 222

Answers (2)

glenn jackman
glenn jackman

Reputation: 246837

$$ 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

Upvotes: 2

slebetman
slebetman

Reputation: 113906

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.]

Upvotes: 5

Related Questions