≡ Menu

Expect Script Tutorial: Expressions, If Conditions, For Loop, and While Loop Examples

Expect scripting language is easy to learn. It expects specific string, and sends (or responds) strings accordingly.

If you are new to expect, read our 6 expect script examples (including hello world example) to get a jump start.

This article explains the following in the expect scripting language.

  • Expressions – arithmetic operation
  • if construct in expect
  • looping constructs

Define Expect Variables – set command

In expect, you can set the variable values using set command as shown below,

# To set the variable with numeric values
set var1 10

# To set the variable with string literal
set name "john"

# To set the variable value which includes output of another command
set proc_id "process id : [pid]"

Note: For expect command line arguments, read 6 Expect Script Command Line Argument Examples.

Expect Expressions – expr command

To evaluate the expressions, use the expr command, which executes the given expression and returns the result. Expect expressions are similar to the C expressions. Some of the valid expressions are listed below.

# To add two simple numerical values
set sum "[expr 1 + 1]"

# To multiple the value of variables
set mul "[expr $sum * $sum]"

# To evaluate conditions and returns 1 or 0 to indicate success or failure accordingly.
set ret "[expr (1+1) == 2]"

# Conditions may contain the command return values.
set ret [expr [pid] == 0]

Expect Conditional Expressions – If command

If command is used for conditional flow of execution of statements as shown in the example below.

if { $count < 0} {
    puts "True : $count\n";
} else {
    puts "False : $count\n";
}

Just like any other programming language, you can use the elseif command in expect as shown below.

if { $count < 0} {
    puts "Success Condition1 : $count\n";
} elseif { $count == 0 } {
    puts "Success Condition2 : $count\n";
} else {
    puts "False : $count\n";
}

Expect Looping Constructs

Expect For Loop Examples:

As we know, for loop is used to do repeated execution of expression until certain condition.

General for loop construct :

for {initialization} {conditions} {incrementation or decrementation} {
...
}

Expect for loop example :

for {set i 1} {$i < $no} {incr i 1} {
 set $total [expr $total * $i ]
}
puts "$total";

Note: You should place the loop open brace in the same line as it contains “for” keyword.

Expect While Loop Examples:

set count 5;
while {$count > 0 } {
puts "count : $count\n";
set count [expr $count-1];
}
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • kashyap January 18, 2011, 4:25 am

    hi vivek,
    I am trying to call a function in a for loop like below:
    #!/bin/bash
    abc()
    {
    /usr/bin/ftp -inv 9<<ENDFTP
    user
    get
    bye
    ENDFTP

    }

    for ((i=0; i<=10; i++ ))
    do
    abc
    done
    the error I am getting is : syntax error: unexpected endof file

  • israel January 18, 2011, 5:19 pm

    Hello, everybody! This is my question
    How can I assign a command output to a variable? For example, the result of any ASCII text manipulation, a result of which could be one lines or one word.
    Thanks in advance,
    israel

  • Francesco Talamona January 23, 2011, 6:40 am

    @Balak
    # To set the variable value which includes output of another command
    set proc_id “process id : [pid]”

    This is wrong, the pid command returns the expect PID: try this and expect will close itself:
    [sko@aemaeth:~]$ expect
    expect1.1> set pid [pid]
    27552
    expect1.2> exec kill -15 $pid

    @Kashyap
    you’re not using expect

    @Israel
    set variablename [exec shellcommand], for example:

    [sko@aemaeth:~]$ expect
    expect1.1> set aaa [exec echo “/bin/ls”]
    /bin/ls
    expect1.2> set b “/tmp/test”
    /tmp/test
    expect1.3> exec $aaa $b
    /tmp/test

  • Arun April 6, 2012, 9:50 am

    This is very good.

    Can you include an example to open more than one session (e.g. 3 telnet sessions) _simultanesously_ and send/receive text with each session?

  • shweta April 23, 2015, 3:47 am

    hi,
    I need 1 help.
    I want to search file with current date and with perticular pattern containing some data ie,greter than 0 bytes.and then paste the content of that file.

    myfile=$(sed -n ‘4,$p’ /tmp/vij/auto/batch_cd.txt)

    for each in `find /opt/sploutput/RMBP5UAT -mtime 1`
    do

    #echo $each

    if read each (find . name *$myfile*_*stderr* – size +0c 2>/dev/null)
    then
    echo “no any log file generated”
    ls -lrt
    fi
    done