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