Bhavin Bhatia
Bhavin Bhatia

Reputation: 43

Expect Script Terminating on its own

I have written the below expect script but it's not working as expected. I want the script to terminate automatically when all the commands are executed. However , the script either never terminates ( if set timeout -1 is used ) or terminates within seconds even before my commands are executed. Can someone please help ?

Here's the script :

#!/usr/local/bin/expect

spawn su vserve
set password vserve
set PWD whoami

set cmdstr(0) "bash /apps/vpn/vserve/vserve_profile"
set cmdstr(1) "bash /apps/vpn/asap/scripts/change_loopback.sh"
set timeout -1
expect "*Password:*" {
sleep 1
send "$password\r"
send "$PWD\r"
sleep 1

for {set i 0} {$i<[array size cmdstr]} {incr i} {
send "$cmdstr($i)\r"

}

send \"exit\r\"
expect eof
}

Upvotes: 0

Views: 673

Answers (1)

pynexj
pynexj

Reputation: 20688

Usually in an interactive shell, you have to expect the specific shell prompt before you send next command. That's the way we make sure the previous command has really finished.

Upvotes: 1

Related Questions