Need help with expect script

I have this very short expect script:

#!/bin/expect

spawn /bin/zypper dup --allow-vendor-change -l
expect "(y): "
send "n\r"
expect eof

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

Use

zypper --no-color ...

I need to wait for some new updates to test this but I have another question
How do i handle the case where

"(y): "

doesn’t appear, this happens when there is “nothing to do” from zypper

Two obvious answers are - you wait 10 seconds or you add additional pattern for “nothing to do”.

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

what i get when there is items to update is:

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?

Looks like you’re running Z shell.

Your closing sentence appears to be incomplete.

I believe the expect executable is in /usr/bin, not /bin

The “waits problem” … disable the timer.

A tutorial is in order - try:

You do not expect to do this :wink: 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.

Easy enough to check this, so no “believe” is needed:

henk@boven:~> which expect
/usr/bin/expect
henk@boven:~>

Try this script, and try it again.

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

Thanks