-1
當我嘗試在期望條件語句中使用變量時,事實證明它不起作用。如何在期望條件語句中執行命令
結果:
[email protected]:~/bin$ ./expectinstall.sh iotop
spawn sudo apt-get install iotop
[sudo] password for qianyu:
我的腳本如下:
#!/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--
我找到我的劇本的問題是沒有得到的用戶名,但設置的用戶名通過`...`
的命令。我將其更改爲[exec ...]
,並且運行良好。
修正後的腳本:
#!/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
參見:如何指望條件語句中使用bash腳本變量] (https://stackoverflow.com/q/11131318/3776858) – Cyrus
@Cyrus,我沒有找到任何線索從我回答這個問題之前的答案。 –
[如何訪問Expect腳本中的環境變量?](https://stackoverflow.com/questions/12695474/how-to-access-environment-variables-in-an-expect-script)可能的重複 – mihir6692