使用`expect`滚动并接受许可协议

Chr*_*ris 5 macos xcode expect

我有一堆Mac刚刚更新了Xcode,需要接受EULA协议.我试图通过脚本来做到这一点.

#!/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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,它永远不会超过第一个期望(也就是说,它永远不会为'Enter'发送"\ r".这是输出:

$ ./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
Run Code Online (Sandbox Code Playgroud)

编辑:更新脚本如下,现在命中超时第二个期望:

#!/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
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 8

这种忽略了使用'except'滚动协议的初始主题,但您也可以同意使用单行的许可.

/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -license accept
Run Code Online (Sandbox Code Playgroud)

这似乎对我有用.


Chr*_*ris 0

使用@Glenn Jackman的信息,我能够解决这个问题。这是我的解决方案,嵌入 bash 脚本中

/usr/bin/expect<<EOF
spawn sudo xcodebuild -license              
expect {
    "*License.rtf" {
        send "\r";
    }
    timeout {
        send_user "\nExpect failed first expect\n";
        exit 1; 
    }
}
expect {
    "*By typing 'agree' you are agreeing" {
        send "agree\r"; 
        send_error "\nUser agreed to EULA\n";
     }
     "*Press 'space' for more, or 'q' to quit*" {
         send "q";
         exp_continue;
     }
     timeout {
         send_error "\nExpect failed second expect\n";
         exit 1;
     }
}
EOF
Run Code Online (Sandbox Code Playgroud)