How to use the Expect in bash scripting?
Here you will find out:
- what the Expect scripting language is
- how to use the AUTOEXPECT command
- examples of using Expect in shell scripts
- when DiskInternals can help you
Are you ready? Let's read!
What is the Expect scripting language?
Much like a bot that automates your scripts, the Expect scripting language works automatically, without needing user interactions. The Expect scripting language requires an “Expecting input,” and then uses the input to run an Expect script without user interaction. However, if you do not install Expect, Expect scripting won’t work on your system. To install Expect on Debian and related distributions, type:
Or, for CentOS:
Expect commands for interactive processes
If you are attempting to run interactive processes, here are the Expect commands you should know about.
- Send: send replies or strings to a process/program
- Spawn: start a script, command, or program
- Expect: wait on the output of a specific process or program
- Interact: chat with your program, set predefined commands
How to use the AUTOEXPECT command
Interestingly, you can set up an automatic Expect shell script using the AUTOEXPECT command. This command is similar to the EXPECT command; the main difference is automation.
You will have to enter your Expect script as a parameter, including all answers to each question on your script to automate its process via the AUTOEXPECT command. The AUTOEXPECT command will save the answers in a new separate file.
The above command will create a new file, called “script.exp”.
Run the new file “script.exp”, and you will get the same answers you expected.
Examples of using Expect in shell scripts
Below are some examples of using expect in Bash scripting:
1. “Hello World”
The script below will expect a specific string, “hello”, in order to send “world” as a response.
2. Setting Timeout On Expect String
The default timeout is 10 seconds, but you can change this setting to any other value.
3. If/Else Conditions
This is how to use if/else clauses in Expect scripts.
Note: Make sure you put the opening braces on the same line.
4. FOR Loops
FOR loops can also be adopted for repetitive tasks. To do so, you must identify three fields:
If you want to open Linux files in Windows easily
Opening Linux files on Windows is, of course, impossible. You need to use special software, and DiskInternals Linux Reader happens to be one of the best software tools for this purpose. DiskInternals Linux Reader features a clean interface that is easy to understand and supports all Linux file systems. Also, this software works on dual-boot systems and virtual machines. It is a comprehensive utility for Linux programmers.
Moreover, DiskInternals Linux Reader is available for free, yet packed with lots of impressive features for your needs.