Search code examples
tclexpect

default path in expect script condition


I'm trying to create an expect script where it will execute every expect condition except the last.

For instance:

send -- "command\r"
expect {
        "No such file or directory" {
            
        }
        "Permission Denied" {
            puts "Command Location 1 execution Failed"
        }
        "finished"
    }

send -- "exit\r"
expect eof
exit 0

Essentially if the expect script detects "No such file or directory" OR "Permission Denied" I want 'puts "Command Location 1 execution Failed"' to be executed rather than having to duplicate in both conditions. However, if "finished" is detected, I don't want anything to be printed, and just have the exit command be sent.

I was thinking the following:

send -- "command\r"
expect {
        "No such file or directory" {
        }
        "Permission Denied" {
        }
        "*" {
         puts "Command Location 1 execution Failed"
        }
        "finished"
    }

send -- "exit\r"
expect eof
exit 0

If only I could have a condition where its like !="finished", that would be perfect.

Any help would be appreciated!


Solution

  • The expect manual suggests using a regular expression with alternation if you don't want to keep repeating the same action. Something like

    send -- "command\r"
    expect {
            -re "(?i)No such file or directory|Permission Denied" {
                puts "Command Location 1 execution Failed"
            }
            finished
    }
    send -- "exit\r"
    expect eof
    exit 0