1

Here is a line from a bash script I am trying to understand:

echo "spawn myscript.sh arg1 arg2; expect \"Please enter your value: \"; send \"myval\r\"; expect eof" | expect

I think I understand the first expect and the send.

Q1: Does expect work only with spawn?

Q2: What is the purpose of the last expect after the pipe?

Q3: In general, can you guess what the original author of this line was attempting to accomplish?

4

Q1: The purpose of expect is to automate interactive programs. To launch the program and interact with it, you use the spawn command.

Q2: the last expect after the pipe is the expect binary. If not given a file argument, it reads its stdin to get the script to execute.

Q3: The author wanted a mechanism to automatically pass a value when the script prompts for one. That's it.

I strongly suspect (depending on what myscript.sh does) that you don't need expect at all:

echo "myval" | myscript.sh arg1 arg2
2

Q1: I'm not an expect user, but it seems clear that spawn runs a script in the background and makes it controllable by expect, rather than just running without any interactivity.

Q2: The string is passed as a script to expect. I assume, like most scripting languages, this is equivalent to saving the string to a file and running expect file.

Q3: It looks like a test to see that myscript.sh, when run, prints a message and responds to a user provided value by exiting without printing anything.

In other words, I expect you should be able to run this script with the same result:

#!/usr/bin/expect
spawn myscript.sh arg1 arg2 # Run `myscript.sh arg1 arg2` and return control to `expect`
expect "Please enter your value: " # Succeeds if the script prints this value
send "myval\r" # Simulate entering this value
expect eof # Succeeds if the script ends without printing anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.