vishal gupta
vishal gupta

Reputation: 41

'if else' statement in Expect script

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?

Upvotes: 4

Views: 29685

Answers (1)

Dinesh
Dinesh

Reputation: 16428

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

Upvotes: 5

Related Questions