【问题标题】:Converting part of read line from file to a variable in tcl将部分读取行从文件转换为 tcl 中的变量
【发布时间】:2021-03-10 18:08:36
【问题描述】:

我有一个类似这样的文件:

IPLIST: 10.10.10.1 10.10.10.2 # A bunch of IPs

#CMDS:
ping $ip

我想使用 tcl 读取文件并运行命令。 我已经成功地读取了文件,并创建了一个 IP 列表,还创建了一个命令列表。 我想运行我所做的命令:

#iplist is the list of IP formed from IPLIST in file
foreach ip $iplist {
 # cmdlist is the list of commands read from file
 foreach cmd $cmdlist {
  echo "$cmd\n"
 }
}

我期待命令中的 $ip 将被第一个 foreach 循环中的 ip 变量替换。但是,这并没有发生。我得到的是:

ping $ip

当我运行 foreach 循环时,有没有办法让文件中的 $ip 转换为 iplist 中的 ip? 我确实在这里查看了一大堆示例,但没有一个可以在这种情况下使用 感谢您的帮助!

【问题讨论】:

    标签: regex file tcl expect


    【解决方案1】:

    尝试使用subst

    echo [subst -nobackslashes -nocommands $cmd]
    

    它将对变量执行替换。我也在使用 -nobackslashes-nocommands 以防万一可能有方括号可能不是您想要执行的,如果有的话,但是如果您确实希望它们执行,那么您可以省略它们.

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2013-11-26
      相关资源
      最近更新 更多