fbpx expect Command on Linux: Automate Script Interactions | Certificação Linux

expect Command on Linux: Automate Terminal Script Interactions

📅 12/07/2021 ⏱ 3 min ✍️ Uira Ribeiro
Linuxcommands
expect Command on Linux: Automate Terminal Script Interactions
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The expect command on Linux and autoexpect can be extremely useful for testing software.

Imagine that you built a program in Shell Script, C, Python, Perl or any other language that runs in the shell and interacts with the user, asking for parameters, and based on the parameters providing responses.

Well, testing this type of program can be tiring, since you will have to type several times some parameters, until your program is working as expected.

The expect command and autoexpect can be useful to do this work.

To illustrate, see the small script that asks for the user's name, then the month of the year:

$ cat script#!/bin/bashecho "Qual seu Nome?"read NOMEecho "Qual mês do ano você quer ver o calendário?"read MESecho "$NOME, aqui vai o calendário:"cal $MES 2020 

To execute the script, it is necessary to make it executable:

$ chmod +x script

When running it:

$ ./script Qual seu Nome?UiráQual mês do ano você quer ver o calendário?01Uirá, aqui vai o calendário:    Janeiro 2020      do se te qu qu se sá            1  2  3  4   5  6  7  8  9 10 11  12 13 14 15 16 17 18  19 20 21 22 23 24 25  26 27 28 29 30 31     

Imagine that you are refining your code, and have to test the program several and several times. Tiring to do this manually.

To create a "self-test" of the execution of this script, without the need to type the name and month each time, you can use the autoexpect command:

$ autoexpect ./scriptautoexpect started, file is script.expQual seu Nome?UiráQual mês do ano você quer ver o calendário?02Uirá, aqui vai o calendário:   Fevereiro 2020     do se te qu qu se sá                     1   2  3  4  5  6  7  8   9 10 11 12 13 14 15  16 17 18 19 20 21 22  23 24 25 26 27 28 29                        autoexpect done, file is script.exp

The autoexpect program creates a file with the script name, followed by the .exp extension.

This generated file is a shell script that uses the expect commands to read the outputs of your program, and send to send the counter responses.

Now, to test the small program, just execute the script.exp script:

$ ./script.exp spawn ./scriptQual seu Nome?UiráQual mês do ano você quer ver o calendário?02Uirá, aqui vai o calendário:   Fevereiro 2020     do se te qu qu se sá                     1   2  3  4  5  6  7  8   9 10 11 12 13 14 15  16 17 18 19 20 21 22  23 24 25 26 27 28 29  

This way, you will no longer need to type anything. Just improve and test your software. 

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp