Expand passed arguments before printing with puts to virtual server

48 Views Asked by At

I am having trouble with an expect script not evaluating arguments. Everything in the puts ${file_id} block (simplified obviously) gets placed onto a virtual machine and is later used to for configuration. The block you see puts the same code into a local directory for me to see if things are working properly.

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

Unfortunately, the log file looks exactly as above. The arguments are not being substituted properly and I can't figure out why. I've tried using regular variables, different syntax for referencing local and global variables but have had no luck getting this to work. Any thoughts?

1

There are 1 best solutions below

1
On BEST ANSWER

As Etan Reisner pointed use double quotes in the puts command instead of braces, so that it will get replaced.

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
}
"

Assuming env(os1) as Ubuntu, will produce the following content in the file

root_image=node-Ubuntu
if {-1 == 0} {
    create_node_byid 1 Ubunt-64
} else {
    create_node_byid 1 Ubuntu-64
}

Note : This will only do variable substitutions not evaluation of code as such. i.e. if-else commands won't be evaluated.