Use `expect` to scroll and accept the license agreement

I have a bunch of Mac computers that have just updated Xcode and must accept the EULA. I am trying to do this through a script.

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

However, it never gets the first wait (that is, it never sends "\ r" to 'Enter'. Here is the result:

$ ./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

EDIT: The script has been updated as follows, now the wait time comes in the second wait:

#!/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
    }
}
+4
source share
5 answers

Using @ Glenn Jackman's information , I was able to solve this problem. Here is my solution built into the bash script

/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
0
source

"except" , .

/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -license accept

.

+8

script, , ,

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

,

, ( ):

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

:

, wait -d , :

!/usr/bin/expect -d

, "", "", "-d".

, , , !

0
source

This works for me to accept the Mac OSX Xcode license. It is almost identical, just adding a bash shell line and providing eod tokens for the wait section. I learned how to call a wait in How to get script output to a file .

#!/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
0
source

All Articles