我想在set命令中使用变量。可以使用吗?当我使用它时无法识别
set MYVAR [lindex $argv 0]
set servername [lindex $argv 1]
set osusername [lindex $argv 2]
spawn ssh "osusername@$servername"
expect "password:"
send "$MYVAR\r"
send "/hyp_util/PasswordChanges/automation/deleteexistingentry.sh\r"
set cmd [format {cat "/home/currentosusername/.ssh/id_rsa.pub" | ssh "$osusername@$servername" 'cat >> /home/$servername/.ssh/authorized_keys'}]
spawn sh -c "$cmd"
expect "password:"
send "$MYVAR\r"
send "exit\r"
interact
没有错误,但它没有识别set命令中的servname
和osusername
变量
set cmd [format {cat "/home/currentosusername/.ssh/id_rsa.pub" | ssh "$osusername@$servername" 'cat >> /home/$servername/.ssh/authorized_keys'}]
答案 0 :(得分:3)
您有以下定义:
set cmd [format {cat "/home/currentosusername/.ssh/id_rsa.pub" | ssh "$osuername@$servername" 'cat >> /home/$servername/.ssh/authorized_keys'}]
和这些替换:
osuername
在这里,您尝试使用osusername
代替servername
,而servname
代替{ }
。名字需要对应。
此外,大括号(format
)会阻止变量替换。您可以使用set cmd [format {cat "/home/currentosusername/.ssh/id_rsa.pub" | ssh "%1$s@%2$s" 'cat >> /home/%2$s/.ssh/authorized_keys'} $osusername $servname]
命令插入值来解决这个问题:
{{1}}
文档: format