anticipate “Directory to backup?r”.
send– “/ home/dave/Documents/ r”.
This reveals the primary 3 commands in expect scripts, the spawn, expect, and send out commands.
The very first thing to discover is the shebang is referring to anticipate, not to Bash. The -f (file) flag informs anticipate the reactions are coming from a file.
We effectively turn the timeouts off by setting them to infinity with -1.
The spawn command introduces the backup script.
These contain the text trigger that will be sent to the terminal window by the “backup.sh” script. When expect sees the timely, the text in the send out line is returned to the “backup.sh” script.
Make both scripts executable:.
chmod +x backup.sh.
chmod +x backup.exp.
set timeout -1.
How expect Works
The anticipate command lets you manage the two ends of a discussion between your set of ready answers and the program or script that youll be sending them to. You do this by producing an “anticipate” script that views for triggers in the main script and sends out the proper action for each one.
Lets say you have a backup script that asks for a source directory site name and a target directory site name. it then makes a copy of the source directory in the target directory. Without the bits that do the file copying, your script might look something like this:
expect “Backup location?r”.
send out– “/ media/dave/external/ backupr”.
The Linux expect command lets you automate interactions with scripts and programs. When it is waiting for some text input, you can send any kind of reaction to the script.
Automation Means Efficiency
When you administer a Linux computer or group of computer systems youll run up versus lots of repeated tasks. The obvious answer is to compose a script to perform the bulk of the work for you. The Bash shell, like all modern shells, supplies a rich and flexible scripting language.
Scripting a long-winded or tiresome task gives you the flexibility to run that task when the office is closed, or in the quietest periods of a 24/7 operation. Scripts likewise give you repeatability. They will not forget to carry out a step, no matter the number of times they are asked to perform the very same chores.
Thats great, as far as it goes. However if your script requires user interaction, or if it calls a program that will require human input, what can you do? You dont want to have to exist when the script runs, or if you are present, to be viewing the terminal window ready to leap in and strike a few keys.
If a program is waiting for a response to a yes or no concern itll be force-fed one of the “y” characters. You need to pipeline the output from yes into the script.
You can do that too, using the yes command.
If it requires to supply more than one different type of reaction, the yes command cant cope. For that circumstance, we need to use expect.
RELATED: How to Use the yes Command on Linux
Installing the expect Command
We checked anticipate on Ubuntu, Fedora, and Manjaro. The plan wasnt bundled with these distributions, so it needed to be set up. On Ubuntu type:
sudo apt install expect
echo “Directory to backup?”.
On Manjaro we utilize pacman:
sudo pacman -Sy expect
echo “Target directory site:” $target_directory.
All this script does is ask for the courses to the two directory sites. It prints the target directory to the terminal window so that we can see that it received a response from expect which it could read it properly.
To supply the other half of the discussion we develop an “expect script.” By convention, these have a “. exp” extension. This example will work with our “backup.sh” script.
#!/ usr/bin/expect -f.
echo “Backup area?”.
On Fedora, the command you require is:
sudo dnf install expect
If your script requires user interaction, or if it calls a program that will need human input, what can you do? You do not desire to have to be present when the script runs, or if you are present, to be viewing the terminal window all set to jump in and strike a few secrets.
Lets state you have a backup script that asks for a source directory site name and a target directory name. These consist of the text trigger that will be sent out to the terminal window by the “backup.sh” script. When anticipate sees the timely, the text in the send line is returned to the “backup.sh” script.
And use the “backup.exp” script to fire off the procedure
You can see the two prompts from “backup.sh” and the responses from “backup.exp.”