在Tcl中,如何防止变量';当传递给“exec”时,shell是否会解释其内容?

在Tcl中,如何防止变量';当传递给“exec”时,shell是否会解释其内容?,tcl,interpolation,io-redirection,csh,Tcl,Interpolation,Io Redirection,Csh,我正在维护一些旧代码,发现下面的代码 if {[catch {exec -- echo $html_email > $file} ret]} { puts $ret return 0 } …由于HTML电子邮件的第一个字符是Tcl的exec而中断,唉。它坚持解释以开头的参数。您可以通过shell间接调用所需的命令: exec -- csh -c "echo '$html_email'" > $file 或 一个do…while循环是合适的,除了Tcl本身没有它们。

我正在维护一些旧代码,发现下面的代码

if {[catch {exec -- echo $html_email > $file} ret]} {
    puts $ret
    return 0
}

…由于HTML电子邮件的第一个字符是
Tcl的
exec
而中断,唉。它坚持解释以
开头的参数。您可以通过shell间接调用所需的命令:

exec -- csh -c "echo '$html_email'" > $file


一个
do…while
循环是合适的,除了Tcl本身没有它们。一个显式的
中断
简化了这一点。这些非常脆弱,因为它们不需要额外的处理就不能很好地处理
字符(在HTML中出现的可能性非常小)。
append html_email "Content-Type       : text/html; charset=us-ascii\n"
append html_email "Content-Disposition: inline\n"
set ctr 0
while 1 {
    set filename /tmp/[pid].[incr ctr].txt
    # POSIX-style flags; write-only, must create or generate error
    if {[catch {open $filename {WRONLY CREAT EXCL}} f] == 0} break
}
puts $f $html_email
close $f
exec echo <$filename >$file
file delete $filename
exec cat <<$html_email >$file
set f [open $file "w"]
puts $f $html_email
close $f
exec -- csh -c "echo '$html_email'" > $file
exec -- csh -c "exec echo '$html_email'" > $file