我有一堆刚更新了Xcode的Mac电脑,需要接受最终用户许可协议。我正在尝试通过脚本完成这个过程。
#!/usr/bin/expect
set timeout 15
spawn sudo xcodebuild -license
expect {
"*License.rtf'\n" { # Press Enter to view agreement
send "\r"
}
timeout {
send_user "\nFailed\n";
exit 1
}
}
expect {
"Software License Agreements Press 'space' for more, or 'q' for quit" {
send_user " ";
exp_continue;
}
"By typing 'agree' you are agreeing" {
send_user "agree\r"
}
timeout {
send_user "\nTimeout 2\n";
exit 1
}
}
然而,它从未通过第一个expect(也就是说,它从未发送“\r”以进行“Enter”)。
以下是输出内容:$ ./test.sh
spawn sudo xcodebuild -license
You have not agreed to the Xcode license agreements. You must agree to both license agreements below in order to use Xcode.
Hit the Enter key to view the license agreements at '/Applications/Xcode.app/Contents/Resources/English.lproj/License.rtf'
Failed
编辑:已更新脚本如下,现在在第二个 expect 处超时:
#!/usr/bin/expect
set timeout 15
spawn sudo xcodebuild -license
expect {
"*License.rtf" {
send "\r"
}
timeout {
send_user "\nFailed\n";
exit 1
}
}
expect {
"By typing 'agree' you are agreeing" {
send "agree\r"
}
"*Press 'space' for more, or 'q' for quit" {
send " ";
exp_continue;
}
timeout {
send_user "\nTimeout 2\n";
exit 1
}
}
send
而不是send_user
来发送空格和 "agree" -- 后者会将其发送到终端而不是生成的程序。 - glenn jackman