Expect script to run shell command

Jagan

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.

Donal Fellows

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.

edited at
0

Comments

0 comments
Login to comment

Related

Run local command from expect script

Run spark-shell command in shell script

How to pass argument in Expect through the command line in a shell script

How to execute command under if statement in Shell Script under expect condition?

SSH + Sudo + Expect in Bash script: Run command with sudo in remote machine

Run Remote Mysql Command with Shell Script

to run a command for various variables in shell script

How to run a shell command from a Meson script?

Linux - Shell script run curl command in parallel

Run ant command from shell script

Shell script won't run cd command

Run script in current shell without . before command

Shell script can be run only with sh command

A shell script to run tabix command on multiple files

Can be run on the command line, but not in a shell script?

How to run sacct command in shell script

Unable to run command in function (shell script)

Shell script to run Linux command on multiple servers command by command

Shell script: run screen, open several screens and run a command in each

How to run a command based on output of another command in shell script?

Linux command that builds a shell script automatically after you run a command

A expect script with multiple shell statements

shell script expect and spawn commands

Run expect script in current directory

[ expect script parsing the command output ]

expect command in bash script failing

Is there any way to send if else statement to remote server while using expect command in shell script?

python + run complicated shell command via python script

How to run a multi command Linux shell script from within R?