The problem is, it waits about 10 seconds before the n\r appears after the
“Continue? [y/n/v/…? shows all options] (y):” prompt.
All I’m trying to do is automate a test to see if there are TW updates available or not.
I’d love to do this in bash but can’t figure out a way to do it so my bash script has to call this short expect script.
TIA
This expect really stumps me. I am trying to do this inside a bash script.
My test/debug script is below and is only for developing, then once it works i will copy the
zypper_test() function to my real script.
#!/bin/bash
zypper_test() {
/bin/expect {
spawn /bin/zypper --no-color dup --allow-vendor-change -l
expect " y): " {
send "n \r"
expect eof }
expect "nothing to do" {
send "\r"
expect eof }
expect eof }
}
zypper_test
# ./z.sh
couldn't read file "{": no such file or directory
./z.sh: line 5: spawn: command not found
couldn't read file " y): ": no such file or directory
./z.sh: line 7: send: command not found
couldn't read file "eof": no such file or directory
couldn't read file "nothing to do": no such file or directory
./z.sh: line 10: send: command not found
couldn't read file "eof": no such file or directory
couldn't read file "eof": no such file or directory
You need to quote arguments to expect (or any other command for that matter) to prevent them from being (mis-)interpreted by shell and you need to tell expect to read commands from its argument and not from some file. I suppose expect has some option to do it. It does have manual page, does not it?
You do not expect to do this Just add to the zypper command:
-n, --non-interactive
Switches to non-interactive mode. In this mode zypper doesn’t ask user to type answers
to various prompts, but uses default answers automatically. Those default answers also
depend on other options like --no-gpg-checks or --ignore-unknown.
#!/usr/bin/expect
set timeout 2 ;# Set a shorter timeout value (in seconds)
spawn /bin/zypper dup --allow-vendor-change -l
expect {
"(y): " {
send "n\r"
exp_continue ;# Continue expecting
}
eof {
exit ;# Exit when the process ends
}
}