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.
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments