简体   繁体   中英

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.

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. That is, if I set a variable in an expect script:

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. The error you're seeing comes from 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.


I'm suspicious that there may be a second problem: you're setting the variable vers to the output of:

rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers

It's not clear why you've got that echo $vers at the end. Since $vers isn't set when you run this command, it's just going to add a blank line to the output. I believe you want simply:

send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$'`"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM