1

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I had tried this with | (pipe) and grep. cant we use | (pipe) in system command?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.