0

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?

2 Answers 2

4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! This worked for me (the first option). You just saved my life :D
1

Can't you just do

set newVar 'lsnrctl status listener_'
append newVar $theHost

1 Comment

I'm planning to make this part also as a variable: lsnrctl status listener_ ... Though your suggestion seems really neat, I dont think I could use it in that situation. Any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.