在使用 puts 打印到虚拟服务器之前扩展传递的参数

Expand passed arguments before printing with puts to virtual server

我在使用不评估参数的 expect 脚本时遇到问题。 puts ${file_id} 块(明显简化)中的所有内容都被放置到虚拟机上,稍后用于配置。您看到的块将相同的代码放入本地目录,以便我查看是否一切正常。

global env
set env(os1) [lindex $argv 0]
set env(scratch_repo) /tmp/yum.repo_[pid]
set file_id [ open ${env(scratch_repo)} "w"]

puts ${file_id} {

root_image=node-${env(os1)}
if {[string first r ${env(os1)}] == 0} {
    create_node_byid 1 [string range ${env(os1)} 0 4]-64
} else {
    create_node_byid 1 [string range ${env(os1)} 0 5]-64
}
}

不幸的是,日志文件看起来与上面完全一样。参数没有被正确替换,我不明白为什么。我试过使用常规变量,引用局部和全局变量的不同语法,但没有运气让它起作用。有什么想法吗?

正如 Etan Reisner 指出的那样,在 puts 命令中使用双引号而不是大括号,这样它就会被替换。

puts ${file_id} "
root_image=node-${env(os1)}
if {[string first r ${env(os1)}] == 0} {
    create_node_byid 1 [string range ${env(os1)} 0 4]-64
} else {
    create_node_byid 1 [string range ${env(os1)} 0 5]-64
}
"

假设env(os1)为Ubuntu,会在文件

中产生如下内容
root_image=node-Ubuntu
if {-1 == 0} {
    create_node_byid 1 Ubunt-64
} else {
    create_node_byid 1 Ubuntu-64
}

注意:这只会进行变量替换而不是代码本身的评估。即 if-else 命令不会被评估。