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

时间:2014-09-30 15:51:58

标签: xcode macos 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
    }
}

但是,它永远不会超过第一个期望(也就是说,它永远不会发送" \ 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

编辑:更新了如下脚本,现在在第二个期望点击超时:

#!/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
    }
}

5 个答案:

答案 0 :(得分:8)

这种忽略了使用'的初始主题,除了'滚动协议,但你也可以同意一个班轮的许可证。

$lastName = $_POST['lastName'];

这似乎对我有用。

答案 1 :(得分:1)

这是最终为我工作的剧本

#!/usr/bin/expect
set timeout 5
spawn sudo xcodebuild -license
send "\r"
expect {
    "By typing 'agree' you are agreeing" {
        send "agree\r\n"
    }
    "Software License Agreements Press 'space' for more, or 'q' to quit" {
        send " ";
        exp_continue;
    }
    timeout {
        send_user "\nTimeout 2\n";
        exit 1
    }
}
expect {
    timeout {
        send_user "\nFailed\n";
        exit 1
    }
}

答案 2 :(得分:0)

对于后代,因为这还没有答案

我最终使用的,对我有用(但可能不是最干净的):

!/usr/bin/expect
set timeout 5
spawn sudo xcodebuild -license
expect {
    "By typing 'agree' you are agreeing" {
        send "agree\r\n"
    }
    "Software License Agreements Press 'space' for more, or 'q' to quit" {
        send " ";
        exp_continue;
    }
    timeout {
        send_user "\nTimeout 2\n";
        exit 1
    }
}
expect {
    timeout {
        send_user "\nFailed\n";
        exit 1
    }
}

我是如何得出这个结论的:

我通过在顶部调用expect -d进行调试,如下所示:

!/usr/bin/expect -d

一旦我发现它正在寻找"同意"而不是" space",我撤销了订单并删除了" -d"。

我很乐意接受任何其他指示,因为这是我第一次玩到期待,并且我对这个工具印象非常深刻!

答案 3 :(得分: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

答案 4 :(得分:0)

这适用于我接受Mac OSX Xcode许可证。它几乎是相同的,只需添加bash shell行并确保期望部分的eod标记。我在How to have Expect script output to file了解了如何调用expect。

#!/bin/bash
echo "TOP: Now start expecting things"

/usr/bin/expect  -f - <<EOD
set timeout 5
spawn sudo xcodebuild -license
send "\r"
expect {
    "By typing 'agree' you are agreeing" {
        send "agree\r\n"
    }
    "Software License Agreements Press 'space' for more, or 'q' to quit" {
        send " ";
        exp_continue;
    }
    timeout {
        send_user "\nTimeout 2\n";
        exit 1
    }
}
expect {
    timeout {
        send_user "\nFailed\n";
        exit 1
    }
}
EOD
相关问题