Jagan
Jagan

Reputation: 159

Expect script to run shell command

How to run a shell command in an expect script and check for some specific string?

#!/usr/bin/expect

set timeout 20

send "ps -aef | grep P1"

expect "string"

Blah 
Blah

exit;

I tried with spawn, exec and system command in place of send, but it always timed out or ended in some error.

Upvotes: 1

Views: 3908

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

The pattern is you spawn the program, expect it to produce some output, send it some input, (repeat the last two as necessary), and close; wait to finish. If the program doesn't produce the expected output, you will wait until it finishes or you get a timeout.

Fortunately, you can wait for multiple things at once:

spawn ps -aef
expect {
    "P1" { ... got it ... }
    eof { ... not got it ... }
    timeout { ... ps hung? ... }
}
close
wait

Upvotes: 2

Related Questions