Friday, October 8, 2010

Expect scritp runs multiple cmds taking as a variable

Hi as part of unit testing i have written a expect script that expects same things but i need to send some list of cmds. For the first time i have written in the sequence of expect, send, sleep and then for the second and go on. But script is very lengthy if i want to see execution flow of cmds i need to scroll down to entire script.
So i find an alternate solution where i kept all my cmds in one varible and split on newline (i used newline, can be changed) and send these cmds one by one using for.
Below script is small example which will explain what i have done. Pretty simple logic and very basic too.
Have a look......

#!/usr/bin/expect -f
set data "ls
cd ..
cd ..
cd /etc
cd /home/baluenigma

#Spwan the Cfm1 Util stub
spawn /bin/bash
match_max 100000

foreach line [split $data \n] {
expect "[baluenigma@ *]#"
send -- "$line\r"
sleep 1

Above data is varible which holds all cmds which need to be executed.
Foreach is used to read data varible and split on newline char and line varible takes that value. Now expect cmd expects "[baluenigma@ *]#" here '*' used because pwd will change when i used cd cmds which are in data variable.

No comments: