'if else' statement in Expect script

18,759

You can simply group them into single expect statement and whichever matched, it can be processed accordingly.

#!/usr/bin/expect
set timeout 60
spawn ssh user@server1
expect "assword" { send "password\r"; }
# We escaped the `$` symbol with backslash to match literal '$' 
# The last '$' sign is to represent end-of-line
set prompt "#|%|>|\\\$ $"
expect {
        "(yes/no)"  {send "yes\r";exp_continue}
        "password:" {send "password\r";exp_continue}
        -re $prompt 
}
send "./jboss.sh status\r"
expect {
        "running" {send "echo running\r"}
        -re $prompt {send "./jboss.sh start \r"}
}
expect -re $prompt
Share:
18,759
vishal gupta
Author by

vishal gupta

Updated on June 04, 2022

Comments

  • vishal gupta
    vishal gupta almost 2 years

    I am trying to create a script which will "send" input as per the output received from executing the previous script.

    #!/usr/bin/expect --
    set timeout 60
    
    spawn ssh user@server1
    
    expect "*assword*" { send "password\r"; }
    
    expect "*$*" { send "./jboss.sh status \r"; }
    if [ expect "*running*" ];
            then { send "echo running \r"; }
    else { send "./jboss.sh start \r"; }
    fi
    

    I am trying to do something like this, but I am stuck in the if else statement. How can I fix it?