Jenson Jose
Jenson Jose

Reputation: 532

How to replace string with variable value in Tcl/Expect

I am trying to replace a specific string (hostname), inside a main string, with a variable value using the following command,

set newVar [string map {`hostname` $theHost} 'lsnrctl status listener_`hostname`']

However, instead of replacing hostname with the variable value, it replaces it using the variable identifier, i.e. "$theHost".

The newVar reads like this,

lsnrctl status listener_$theHost

I want it to be this,

lsnrctl status listener_theHostName

where "theHostName" is the value of the variable "$theHost".

How can I achieve this?

Upvotes: 0

Views: 5169

Answers (2)

Johannes Kuhn
Johannes Kuhn

Reputation: 15163

Build the replace list with the listcommand:

set newVar [string map [list `hostname` $theHost] "lsnrctl status listener_`hostname`"

or you could just use

set newVar "lsnrctl status listener_${theHost}"

' has no special meaning in Tcl, so your snippet will throw an error. use " if you want command, variable and backslash substitution, { if you don't want this. (\ before the end of a line will be still substituted).

So using {`hostname` $theHost} will be substituted to `hostname` $theHost, not the thing you want. So better build a list with list, "`hostname` $theHost" might work if $theHost is a valid list element (does not contain spaces, etc), but will fail if the value of $theHost is foo bar (string map will throw an error)

Upvotes: 4

nneonneo
nneonneo

Reputation: 179392

Can't you just do

set newVar 'lsnrctl status listener_'
append newVar $theHost

Upvotes: 1

Related Questions