+ All Categories
Home > Documents > TCL Scripting Session 1

TCL Scripting Session 1

Date post: 03-Jun-2018
Category:
Upload: shivamupadhyay
View: 231 times
Download: 0 times
Share this document with a friend

of 20

Transcript
  • 8/12/2019 TCL Scripting Session 1

    1/20

    An Introduction To TclScripting

    SESSION - 1

    - Akhilesh Mahajan

  • 8/12/2019 TCL Scripting Session 1

    2/20

    Todays Agenda

    Why TCL? TCL Commands

    Variables Declaration

    Substitution

    Basics of Lists

  • 8/12/2019 TCL Scripting Session 1

    3/20

    Why TCL?

    Simplicity

    GUI

    Development Tools

    Internationalization

    Extensibility

    Embed ability

    Compatibility

    Event Handling

  • 8/12/2019 TCL Scripting Session 1

    4/20

    Comparison Chart

  • 8/12/2019 TCL Scripting Session 1

    5/20

    Getting Started Tcl programs are usually called "scripts"

    The binaries and libraries of Tcl/Tk and its

    extensions are generally installed under /usr/bin

    and /usr/lib.

    You need to set up a few environment variables

    in your .cshrc/.bashrc

    setenv TCL_LIBRARY ~/tcl/itcl/lib/tcl7.4

    setenv TK_LIBRARY ~/tcl/itcl/lib/tk4.0setenv ITCL_LIBRARY ~/tcl/itcl/lib/itcl2.0

    setenv ITK_LIBRARY ~/tcl/itcl/lib/itk2.0

    setenv EXPECT_LIBRARY ~/tcl/expect/lib

  • 8/12/2019 TCL Scripting Session 1

    6/20

    TCL Commands

    Built-in Commands

    Extensions

    Procedures

    Parser

    Init

    CommandLoop

    ApplicationCommands

    Built-InCommands

    Tcl Application

    ExtensionCommands

    Extension

  • 8/12/2019 TCL Scripting Session 1

    7/20

    Variable Declaration

    Set varName ?value?

    e.g. set x 40

    Same command is use to Read and Write You don't need to declare variables

    Variables created automatically

    Variables do not have typesAny variable can hold any value

  • 8/12/2019 TCL Scripting Session 1

    8/20

    Variable Declaration (Cont.)

    Other Examples:set a {Eggs: $3.5/dozen}Eggs: $3.5/dozen

    set i 3

    set a [expr $i + 2]5

    set msg Starent Networks

    Starent Networks

    Unset a Variable

    unset ?varname?

  • 8/12/2019 TCL Scripting Session 1

    9/20

    Substitution

    TCL provides THREE types ofsubstitution

    1. Variable Substitution2. Command Substitution

    3. Backslash Substitution

    Substitution always occurs from left toright

  • 8/12/2019 TCL Scripting Session 1

    10/20

    Variable Substitution

    Syntax: $varName

    Variable names are letters, digits, underscores.

    May occur anywhere in a word.

    Sample command Result

    set b 66 66set a b bset a $b 66

    set a $b+$b+$b 66+66+66set a $b.3 66.3set a $b4 no such var iable

  • 8/12/2019 TCL Scripting Session 1

    11/20

    Command Substitution

    Syntax: [scr ip t]

    Evaluate script, substitute result.

    May occur anywhere within a word.

    Sample command Result

    set b 8 8

    set a [expr $b+2] 10set a "b-3 is [expr $b-3]" b-3 is 5

  • 8/12/2019 TCL Scripting Session 1

    12/20

    Backslash Substitution

    Used to insert special characters such as

    newline

    [ ]

    $

    without them being treated specially by TCL

    parser.

    set msg Eggs:\ \$2.18/dozen\nGasoline:\\$1.49/gallon

    Eggs: $2.18/dozen

    Gasoline: $1.49/gallon

  • 8/12/2019 TCL Scripting Session 1

    13/20

    Quoting

    Words break at white space and semi-colons, except:

    Double-quotes prevent breaks:set a "x is $x; y is $y"

    Curly braces prevent breaks and substitutions:set a {[expr $b*$c]}

    Backslashes quote special characters:set a word\ with\ \$\ and\ space

  • 8/12/2019 TCL Scripting Session 1

    14/20

    Expressions expr arg ?arg arg ...?

    Concatenates all the arg values together (with spaces inbetween), evaluates the result as an expression, and returns

    a string corresponding to the expressions value.

    Sample command Resultset b 5 5incr b -1 4expr ($b*4) - 3 17

    expr $b

  • 8/12/2019 TCL Scripting Session 1

    15/20

    Operators

    Available Operators:

    Arithmetic Operators

    Relational Operators

    Logical Operators

    Bitwise Operators

    Choice Operators

    Identical to C, C++, Java.

  • 8/12/2019 TCL Scripting Session 1

    16/20

    List Lists are used in Tcl to deal with collections of things

    1. such as all the users in a group or

    2. all the files in a directory

    Lists allow you to collect together anynumber of values in one place, pass

    around the collection as a single entity,

    and later get the component values back

    again

  • 8/12/2019 TCL Scripting Session 1

    17/20

    Creating ListFour ways to create a list

    Set x {a b c d e} or

    Set y a b c d e or

    Set z [list a b c d e]Set x [concat $x $y $z]

    concatremoves one level of grouping beforeforming the list, while listworks directly from

    the original arguments

  • 8/12/2019 TCL Scripting Session 1

    18/20

    List and Concat

    The command

    lista b "c d e " " f {g h}"

    will returna b {c d e } { f {g h}}

    while concatwith the same arguments will

    returna b c d e f {g h}

  • 8/12/2019 TCL Scripting Session 1

    19/20

    Split and Join

    Split a string into a proper Tcl list

    Join create a string by joining list elementstogether

    Set dir /usr/bin/radius

    Set abc [Split $dir /]

    usr bin radius

    Set abc [join $abc ;]

    usr;bin;radius

  • 8/12/2019 TCL Scripting Session 1

    20/20

    Operation on List

    lappend

    lindex

    linsert

    llength

    lrange

    lreplace

    lsearch

    lsort

    lrepeat


Recommended