TCL and Expect

Embed Size (px)

Citation preview

  • 8/16/2019 TCL and Expect

    1/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    TCL and Expect

    William Tracy

    Thursday, February 19, 2009

    William Tracy   TCL and Expect

  • 8/16/2019 TCL and Expect

    2/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    What is TCL?

     Unix scripting language

      Intended for embedding in applications

      Shell-like syntax

     Low memory footprint

    William Tracy   TCL and Expect

  • 8/16/2019 TCL and Expect

    3/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Input, Output, and Variables

    Listing 1: File 0-io

    1   #! / u s r / b i n / t c l s h23   s e t   l a n g u a g e TCL4   puts   ” H e l l o , $ l an g ua g e ! ”5   puts   { H e l l o , $ la n g ua ge ! }

    67   puts   s td o u t ” E n t e r a s t r i n g : ”8   g e t s   s t d i n i n p u t9   puts   s t d o ut ”You s a i d $ i n p u t ! ”

    William Tracy   TCL and Expect

  • 8/16/2019 TCL and Expect

    4/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Control flow

    Listing 2: File 1-if 

    1   #! / u s r / b i n / t c l s h23   s e t   f l a g t r u e4   i f    { $ f l a g } {

    5   puts   Tru e !6   }   e l s e   {7   puts   F a l s e !8   }

    William Tracy   TCL and Expect

    I d i TCL

  • 8/16/2019 TCL and Expect

    5/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Loops

    Listing 3: File 2-loops

    1   #! / u s r / b i n / t c l s h

    23   s e t   i n p u t n4   w h i l e   { $ i np u t != y } {5   puts   ” Would you l i k e t o q u i t ?   ”

    6   g e t s   s t d i n i n p u t7   }89   f o r   { s e t   i 0} { $ i   <   10} { i n c r   i } {

    10   puts   $ i

    11   }William Tracy   TCL and Expect

    I t d ti t TCL

  • 8/16/2019 TCL and Expect

    6/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Example

    Listing 4: File install1   #!/ us r / bi n / t c l s h2

    3   s e t   f a i l t r u e45   p u t s   ” I n s t a l l o p t i o n a l c o mp o ne n ts ?   ”6   g e t s   s t d i n d at a7   i f    { $ d at a != y} {8   e x i t9   }

    1011   i f    { $ f a i l } {

    12   p u t s   ” H ar dw ar e n o t s u p p o r t e d . I n s t a l l w i l l c a u s e s p o n t a n e o u s ”13   p u t s   ” c o m b u s t i on . C o nt i nu e ?   ”14   g e t s   s t d i n d at a15   i f    { $ d at a != y} {16   e x i t17   }18   }1920   p u t s   ” I n s t a l l w i l l t a k e 1 0 G b. C o n t i n u e ?   ”

    21   g e t s   s t d i n d at a

    William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    7/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Whitespace

    Listing 5: File 3-ifbroken1   #! / u s r / b i n / t c l s h23   s e t   f l a g t r u e4   i f    { $ f l a g }

    5   {6   puts   Yay!7   }

    William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    8/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Whitespace

    Listing 6: File 4-iffixed

    1   #! / u s r / b i n / t c l s h23   s e t   f l a g t r u e4   i f    { $ f l a g } {5   puts   Yay!

    6   }7   i f    { $ f l a g } \8   {9   puts   Yay!

    10   }

    William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    9/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Whitespace

    Listing 7: File 5-comments

    1   #! / u s r / b i n / t c l s h23   puts   ” B l a b l a ”   # Witt y comment 

    45   puts   ” B l a b l a ” ;   # Witt y comment 

    William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    10/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Grouping

     Quotes group arguments  Braces disable substitution in a group

     Square brackets are replaced by the execution of thatcommand

    Listing 8: File 6-bracketsputs   [ expr   2 + 2 ]

    William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    11/19

    Introduction to TCLTCL Fundamentals

    TCL odditiesMore TCL syntax

    Introduction to ExpectExpect fundamentals

    Expect examplesThe end

    Data Structures

    Listing 9: File 7-structures

    1   #! / u s r / b i n / t c l s h

    23   s e t   m y l i s t   {a b c d}4   puts   $ m y l i s t5   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 ]78   s e t   m y a rra y ( f o o ) b a r9   s e t   myarr ay ( bazz ) buzz

    10 p a r r a y m ya rr ay

    11   puts   $m ya rr ay ( fo o )William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    12/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect examples

    The end

    Procedures

    Listing 10: File 8-proc

    1   #! / u s r / b i n / t c l s h23   proc   sum   { a rg 1 a rg 2 } {4   s e t   x [ expr   { $ a r g 1 + $ a r g 2 } ]

    5   r e t u r n   $x6   }7   puts   ”The sum o f 2 + 3 i s : [ sum 2 3 ] ”

    William Tracy   TCL and Expect

    Introduction to TCL

  • 8/16/2019 TCL and Expect

    13/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect 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

  • 8/16/2019 TCL and Expect

    14/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect 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 at 9−spawn

    3   puts   $ s p a w n i d

    William Tracy   TCL and Expect

    Introduction to TCLTCL F d l

  • 8/16/2019 TCL and Expect

    15/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect 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 −expect3 e x p e c t e xp e c t

    4   puts   $ e x p e c t o u t ( b u f f e r )5 e xp ec t $ s p a w n i d   e o f 

    William Tracy   TCL and Expect

    Introduction to TCLTCL F d t l

  • 8/16/2019 TCL and Expect

    16/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect examples

    The end

    Send

    Listing 13: File 11-send1   #! / u s r / b i n / e x p e c t  23 spawn b an ne r4 e x p e c t M e ss a ge :5 s e n d :− ) \ n6 e x p e c t   e o f 

    William Tracy   TCL and Expect

    Introduction to TCLTCL Fundamentals

  • 8/16/2019 TCL and Expect

    17/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect examples

    The end

    Autoinstall

    Listing 14: autoinstall1   #!/ usr / bi n / ex pe ct  23 s p aw n . / i n s t a l l45 e x p e c t ” I n s t a l l o p t i o n a l c om po ne nt s ?   ”6 s e n d y\n78 e x pe c t ” c o mb us ti on . C on ti nu e ?   ”   {9 s e n d ”n\n”

    10   }   ” I n s t a l l w i l l t a k e 1 0 G b. C o n t i n u e ?   ”   {11 s e n d y\n12   }1314 e xp e c t   e o f 

    William Tracy   TCL and Expect

    Introduction to TCLTCL Fundamentals

  • 8/16/2019 TCL and Expect

    18/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect examples

    The end

    Hunt the Wumpus

    William Tracy   TCL and Expect

    Introduction to TCLTCL Fundamentals

  • 8/16/2019 TCL and Expect

    19/19

    TCL FundamentalsTCL oddities

    More TCL syntaxIntroduction to Expect

    Expect fundamentalsExpect examples

    The end

    The book

    William Tracy   TCL and Expect