-1

When I try to use variable in an expect condition statement, it turns out that it doesn't work.

result:

qianyu@ubuntu:~/bin$ ./expectinstall.sh iotop
spawn sudo apt-get install iotop
[sudo] password for qianyu:

my script below:

#!/usr/bin/expect -f

set timeout -1
set password qianyu
set app [lindex $argv 0]
set username `whoami`

spawn sudo apt-get install $app
expect {
"*password for ${username}*" { send "${password}\r" }
}
expect eof

--update--

I find the problem of my script is not getting the username, but setting the username by command in `...`. I changed it to [exec ...], and it works well.

corrected script:

#!/usr/bin/expect -f

set timeout -1
set password qianyu
set app [lindex $argv 0]
set username [exec whoami]

spawn sudo apt-get install -y $app 
expect {
"*password for ${username}*" { send "${password}\r"; }
}
expect eof
Shenme Yiwei
  • 11
  • 1
  • 4
  • 1
    See: [How to use bash script variables in expect conditional statements](https://stackoverflow.com/q/11131318/3776858) – Cyrus Jun 03 '17 at 09:52
  • @Cyrus, i didn't find any clue from the answer before i post this question. – Shenme Yiwei Jun 03 '17 at 10:23
  • Possible duplicate of [How to access environment variables in an Expect script?](https://stackoverflow.com/questions/12695474/how-to-access-environment-variables-in-an-expect-script) – mihir6692 Jun 06 '17 at 05:49

1 Answers1

0

Prefix it with $env(...)
As an example, to use $HOME, do this
$env(HOME)

mihir6692
  • 177
  • 1
  • 4
  • 19