Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
TCL and Expect
William Tracy
Thursday, February 19, 2009
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
What is TCL?
Unix scripting language
Intended for embedding in applications
Shell-like syntax
Low memory footprint
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Input, Output, and Variables
Listing 1: File 0-io
1 #! / u s r / b i n / t c l s h
2
3 s e t l a n g u a g e TCL
4 puts H e l l o , $ l a n g u a g e !
5 puts { H e l l o , $ l a n g u a g e ! }
6
7 puts s t d o u t E n t e r a s t r i n g :
8 gets s t d i n i n p u t
9 puts s t d o u t You s a i d $ i n p u t !
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Control flow
Listing 2: File 1-if
1 #! / u s r / b i n / t c l s h
2
3 set f l a g true
4 if { $flag } {
5
puts True !
6 } else {
7
puts F a l s e !
8 }
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Loops
Listing 3: File 2-loops
1 #! / u s r / b i n / t c l s h
2
3 set input n
4 w h i l e { $ i n p u t != y } {
5
puts Would you l i k e t o q u i t ? <y /n>
6
gets s t d i n i n p u t
7 }
8
9 f o r { s e t i 0} { $ i < 10} { i n c r i } {
10
puts $ i
11 }
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Example
Listing 4: File install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#! / u s r / b i n / t c l s h
set
fail
true
p u t s I n s t a l l o p t i o n a l components ? <y /n>
gets s t d i n data
i f { $ d a t a != y} {
exit
}
if {$fail} {
p u t s Hardware n o t s u p p o r t e d . I n s t a l l
p u t s c o m b u s t i o n . C o n t i n u e ? <y /n>
gets s t d i n data
i f { $ d a t a != y} {
exit
}
}
w i l l cause spontaneous
p u t s I n s t a l l w i l l t a k e 10 Gb. C o n t i n u e ? <y /n>
gets s t d i n data
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Whitespace
Listing 5: File 3-ifbroken
1 #! / u s r / b i n / t c l s h
2
3 set f l a g true
4 if { $flag }
5 {
6
puts Yay !
7 }
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Whitespace
Listing 6: File 4-iffixed
1
2
3
4
5
6
7
8
9
10
#! / u s r / b i n / t c l s h
set f l a g true
if { $flag } {
puts Yay !
}
if { $flag } \
{
puts Yay !
}
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Whitespace
Listing 7: File 5-comments
1 #! / u s r / b i n / t c l s h
2
3 puts B l a b l a # W i t t y comment
4
5 puts B l a b l a ; # W i t t y comment
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Grouping
Quotes group arguments
Braces disable substitution in a group
Square brackets are replaced by the execution of that
command
Listing 8: File 6-brackets
puts [ expr 2 + 2 ]
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Data Structures
Listing 9: File 7-structures
1 #! / u s r / b i n / t c l s h
2
3 s e t m y l i s t { a b c d}
4 puts $ m y l i s t
5 puts [ l i n d e x $ m y l i s t 0 ]
6 puts [ l l e n g t h $ m y l i s t ]
7
8 set myarray ( foo ) bar
9 s e t m y a r r a y ( b a z z ) bu zz
10 p a r r a y m y a r r a y
11 puts $ m y a r r a y ( f o o )
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Procedures
Listing 10: File 8-proc
1 #! / u s r / b i n / t c l s h
2
3 proc sum { a r g 1 a r g 2 } {
4
s e t x [ expr { $ a r g 1 + $ a r g 2 } ]
5
r e t u r n $x
6 }
7 puts The sum o f 2 + 3 i s : [ sum 2 3 ]
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
What is Expect?
Extension to TCL
Spawns and manages child processes
Handles stdin and stdout of children
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Spawn
Listing 11: File 9-spawn
1 #! / u s r / b i n / e x p e c t
2 spawn c a t 9spawn
3 puts $ s p a w n i d
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Expect
Listing 12: File 10-expect
1 #! / u s r / b i n / e x p e c t
2 spawn c a t 10 expect
3 expect expect
4 puts $ e x p e c t o u t ( b u f f e r )
5 e x p e c t $spawn id eof
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Send
Listing 13: File 11-send
1 #! / u s r / b i n / e x p e c t
2
3 spawn b a n n e r
4 expect Message:
5 s e n d : ) \ n
6 e x p e c t e of
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Autoinstall
Listing 14: autoinstall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! / u s r / b i n / e x p e c t
spawn . / i n s t a l l
expect I n s t a l l
s e n d y\n
o p t i o n a l components ? <y /n>
e x p e c t c o m b u s t i o n . C o n t i n u e ? <y /n> {
s e n d n\n
} I n s t a l l w i l l t a k e 10 Gb. C o n t i n u e ? <y /n> {
s e n d y\n
}
expect eof
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
Hunt the Wumpus
William Tracy
TCL and Expect
Introduction to TCL
TCL Fundamentals
TCL oddities
More TCL syntax
Introduction to Expect
Expect fundamentals
Expect examples
The end
The book
William Tracy
TCL and Expect