Do expect script have "or" statement?

Do expect script have "or" statement?

sound like this:

expect {
    "A" or "B" {
        send "123"
    }
    "C" {
        send "567"
    }

I know expect can separate that to do this

expect {
    "A" { send "123" }
    "B" { send "123" }
    "C" { send "567" }
}

when the expect script getting longer
I need to copy the same part so many times to do that
It is too hard to maintain:wall:

anyone got an idea about that?:confused:

expect {
    "\[AB]" {
        send "123"
    }
    "C" {
        send "567"
    }

If you real patterns are more complex, you'll need a different solution.

1 Like

I know it does not have everything to do with your question, but have you seen the autoexpect command? Check this link: autoexpect(1) - Linux man page

It may help you...

2 Likes

Thanks for you solution.

And I got some question about that...
Is that just support one character of change inside each square brackets?

How can I define "with space" or "no space" case?
In my situation, I need to login to difference server by using that same expect script. Some of the server using "ftp>", and some using "ftp >"
As your suggestion, I'm trying to expect "ftp\>"
but it seem doesn't work in this case. Am I missing something?:eek:

This should be sufficient:

"ftp*>" 
1 Like

any other solution?
I prefer expect "ftp>" and "ftp >" exactly.
because the output maybe include "ftp" word
and I don't want to separate it :wall:

Yes:

expect {
    -re "ftp ?>" {
        send "123"
    }
    ...
  }
1 Like

:eek: so, I can use Regular Expression to solve the complex case like this:cool:

expect {
    -re "ABC|"BCD" {
        send "123"
    }
    "KKK" {
        send "567"
    }

Thankyou:b:

Thanks for that... :slight_smile:

--ahamed