[英]How can I extract the OS version of a remote VM and set it as an environment variable on the remote VM and also to use further in the expect script?
I am trying to fetch the OS version of a remote VM and add it as an environment variable on the same vm and also use the fetched value further on the host vm from where the expect script is being executed. 我试图获取远程VM的OS版本并将其作为环境变量添加到同一个vm上,并且还在执行expect脚本的主机vm上进一步使用获取的值。
I am trying to export the value of the fetched value. 我试图导出获取值的值。 But the variable is not being set.
但是变量没有被设定。
send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers`\r"
expect -re $prompt
I am seeing below issue: 我看到下面的问题:
can't read "vers": no such variable
while executing
"send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers`\r""
Value is set into the variable on the remote vm and I should be able to use it in the expect script. 值被设置到远程虚拟机上的变量中,我应该能够在期望脚本中使用它。
There are a couple of problems here. 这里有几个问题。 First, recall that
expect
uses the same syntax as sh
for variable expression. 首先,回想一下,
expect
对变量表达使用与sh
相同的语法。 That is, if I set a variable in an expect
script: 也就是说,如果我在
expect
脚本中设置一个变量:
set somevar "somevalue"
I can print it out by running: 我可以通过运行打印出来:
puts "$somevar"
This means that when you have a command like this... 这意味着当你有这样的命令时......
send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers`\r"
...then expect will attempt to expand the $vers
variable before sending that text to the connected process. ...然后期望在将该文本发送到连接的进程之前尝试扩展
$vers
变量。 The error you're seeing comes from expect
; 你看到的错误来自于
expect
; we can reproduce it like this: 我们可以像这样重现它:
expect1.7> puts "$var_that_does_not_exist"
can't read "var_that_does_not_exist": no such variable
You will need to escape the $
in your string, like this: 您需要在字符串中转义
$
,如下所示:
send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo \$vers`\r"
That will send the intended command to your shell. 这会将预期的命令发送到你的shell。
I'm suspicious that there may be a second problem: you're setting the variable vers
to the output of: 我怀疑可能存在第二个问题:您将变量
vers
设置为以下输出:
rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers
It's not clear why you've got that echo $vers
at the end. 目前尚不清楚为什么你最后会得到那个
echo $vers
。 Since $vers
isn't set when you run this command, it's just going to add a blank line to the output. 由于运行此命令时未设置
$vers
,因此它只是在输出中添加一个空行。 I believe you want simply: 我相信你想简单地说:
send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$'`"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.