How can I use a variable in if condition in expect script?


April 2019


6 time


I am executing expect script on a remote host(say A) and I want to fetch some environment variables from that remote host(A). Depending on the remote host's(A) environment variables, I would like to perform some conditional operations on that host(A) and on the host(B) from which the expect script is being run.

I could fetch the remote variables and set values in the remote variables. I couldn't execute the if condition as may be I am having issues figuring out the right syntax/format.

Tried some from the google references but couldn't really get any closer to a working solution.

send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$'`\r"
send "echo \$vers\r"
expect -re $prompt
send "`if [[ \$vers -lt 7 ]]; then echo 'RHEL Version is \$vers'; else echo 'RHEL Version is \$vers'; fi`\r"

Getting below error:

invalid command name "$vers"
    while executing
"\$vers -lt 7 "
    invoked from within
"[ \$vers -lt 7 ]"
    invoked from within
"send "`if [[ \$vers -lt 7 ]]; then echo 'RHEL Version is \$vers'; else echo 'RHEL Version is \$vers'; fi`\r""

Need the expect script to execute "if" condition correctly and pass the value to the remote host and my local.

0 answers