TCL scripting

 TCL scripting

Tcl uses most of the tcsh commands but the syntax varies.

We shall look into tcl commands:

  1. Assigning a value to a variable:

Set a <value>

Eg: set a 10

This prints 10 in the prompt.

  1. To print the value of variable :

Puts <channel ID> $variable

Eg: set a 10

Puts stdout $a

Output: 10


  1. To get input of a variable in terminal:

Gets <stdin> variable

Eg: gets stdin a

%10

Output: 2  

It prints me length of characters of a given input.


  1. Ways of assigning a list to variable:

Set a “list of values”

Set a {list of values}

Set a [list <list of values>]

Eg: set a “10 20 30”

Eg: set a {10 20 30}

Eg: set [list 10 20 30]


  1. Command substitution:

Command substitution is used for using a command in another command.

Eg: puts [expr 10 + 20]

Output: 30

The command used for command substitution is: []


  1. Split and join command:

Set a {xor_or and nand_nor xnor_not}

Split $a “_”

Output: {xor} {or and nand} {nor xnor} {not}

Join $a “_”

Output: xor_or_and_nand_nor_xnor_not



  1. Mathematical operations:

Expr is used to all the maths operation.

Eg: set a 10

Set b 20

Puts [expr $a + $b]

Output: 30

  1. Expr{abs(x)}

Eg: set a -10

Expr{abs($a)}

Output: 10

It prints me the positive values.

  1. Expr{ceil(x)}

It rounds to highest value of given number.

Eg: set a 10.5

Expr{ceil($a)}

Output: 11

  1. Expr{floor(x)}

It rounds to lowest value of given number.

Eg: set a 10.5

Expr{floor($a)}

Output: 10

  1. Expr{double(x)}

It prints me floating value.

Eg: set a 10

Expr{double($a)}

Output: 10.0

  1. Expr {int(x)}

It prints me the integer value of given number.

Eg: set a 10.5

Expr{int($a)}

Output: 10

  1. Expr{min(range of values)}

Eg: expr{min(10 30 9 50)}

Output: 9


  1. Expr{max(range of values)}

Eg: expr{max(10 20 3 50)}

Output: 50

  1. Expr{pow(x,y)}

Eg: expr{2,2})

Output: 4


  1. List operations:

We have few list operations where we can modify a list.

  1. Lindex:

Eg: Set a {10 20 30 40}

Lindex $a 0

Output: 10

The index values of a list starts with 0.

We can access last index of a list using “end.

Lindex $a end

Output: 40

  1. Llength:

It prints me the length of a list.

Eg: set a {10 20 30 40}

Llength $a 

Output: 4

  1. Lappend:

It appends a value to the list.

It modifies the original list.

Eg: set a {10 20 30 40}

Lappend a xor

Output: 10 20 30 40 xor



  1. Linsert:

It inserts given value at the given index.

Eg: set a {10 20 30 40 50}

Linsert $a 2 and

Output: 10 20 and 30 40 50

  1. Lsort:

It sorts the given list with respect to ascii character, we can you use few switches to sort.

Eg: set a {10 20 AND and 30 AND 40}

Lsort –increasing –unique $a

Output: 10 20 30 40 AND and

-increasing: it sorts in increasing order

-decreasing: it sorts in decreasing order

-unique: it does not print the repeated values in the list. 

  1. Lset:

It sets a value to the given index. It modifies the original list.

Eg: set a {10 20 30 40}

Lset a 2 xor

Output: 10 20 xor 40

  1. Lassign:

It assigns the values in the list to another variable.

Eg: lassign {10 20 30} a b c

Puts “$a $b $c”

Output: 10 20 30

  1. Lreplace:

It replaces the range of values or a value of the list.

Eg: set a {10 20 30 40 50 60 70}

Lreplace $a 0 0 xor

Output: xor 20 30 40

Lreplace $a 2 4 xor

Output: 10 20 xor 60 70

Lreplace $a 1 1

Output: 10  30 40 50 60 70

  1. Lsearch:

It searches given pattern.

Eg:

Lsearch {a b c}.c

Output: 2

Lsearch –all {a b c bba abb c}.c

Output: 2 5

Lsearch –inline {a b c bba abba c abb}b*

Output: b

Lsearch –inline –all {a b c abba bba bab}b*

Output: b bba bab

  1. Lrange:

It prints range of values.

Eg: set a {10 20 30 40 50 60}

Lrange $a 0 0

Output: 10

Lrange $a 0 3

Output: 10 20 30 40



  1. Arrays in tcl:

Arrays are used as single dimension list of values where we can assign it to one variable there by reducing the memory space.

We can assign values to arrays using two methods:

  1. Conventional method:

Set variable(key/index) value

Eg: set a(ones) {1 2 3 4}

Here in the example “a” is my array name, “ones “ is my key of the array where the given list gets stored.

  1. Associative method:

This is the most used method of assigning values to the array.

There few sets in this method, let’s look at them.

  1. To set values to an array:

Syntax: array set array_name {

Keys {list}

}

Eg: array set numbers { 

Ones {1 2 3 4}

Tens {10 20 30 40}

}

  1. To print the array elements:

Syntax: puts stdout $array_name(key)

  1. To print array elements using array:

Syntax: array get array_name : this prints all the keys and their elements.

Syntax: array get array_name key: this prints only the elements of the array of that particular key.

  1. To print size of the array:

Syntax: array size array_name 

  1. To print only the elements of the array:

Syntax: array names array_name 

  1. To unset the array:

Syntax: array unset array_name

  1. To check if array exists or not:

Syntax: array exists array_name: if yes it prints 1 if no it prints 0.


  1. Loops in tcl:

    1. For loop:

Syntax: for { initialization } { condition } { increment/decrement } { statement }

Eg: for { set a 1 } { $a <= 5 } { incr a } {puts $a }

Output: 1

    2

    3

    4

    5




  1. While loop:

Syntax: initialize while { condition } { statement ; increment/decrement}

Eg: set a 1

while { $a <= 5 } { puts $a incr a}

Output: 1

    2

    3

    4

    5

  1. Foreach loop:

Syntax: foreach loop_variable  variable {statement}

Eg: set a {10 20 30 40}

Foreach i $a {

Puts $i

}


d. Catch: It captures errors. If we run a set of commands from a file and some error has occurred, capturing those errors will be helpful so that we can grep the error and correct the command.

syntax: catch command arguments

if {[catch {command arg..} result ] } {

puts stderr $result

} else {

puts “no errors”

}









  1. Conditional statements:

    1. If { condition } {

Statement

}

If { condition } {

Statement 

}

  1. If { condition } {

Statement

} elseif { condition } {

Statement

} else {

Statement

}

  1. If { condition } {

Statement 

} else {

Statement

}

  1. Switch string {

Statement 1

}

String 2 {

Statement 2

}

Default {statement}

}

}


  1. Loop control statements:

    1. Break

    2. Continue

    3. Exit


  1. Procedures:

It is a block of code with a series of commands that provide specific reusable functions. This is similar to the functions that we have learnt in C language.

It has two parts: definition and calling.

Syntax: proc procedure_name {arguments} {

    Body

    }

  <calling the proc>

Eg: proc hello{}{

      Puts “hello world”

}

Hello

Output: hello world




Scope of the procedure:

It has 3 types of scopes: local, global and upvar.

  1. Local scope:

Eg: proc test { } {

      Set x 4

      Puts “x is $x”

      }

     Set x 1

      Puts “x is $x”

      Puts {}

     test

      Puts { }

      Puts { }

      Puts “x is $x”

Output: x is 1 

               X is 4

  X is 1

Therefore, when x = 4 it is local scope and has to be called inside the definition only.

  1. Upvar scope:

Variable that is outside or one level up.

Syntax: upvar <variable outside> <variable inside procedure>

Eg: proc test { } {

      Upvar x y

      Puts “y is $y”

      Set y 4

      Puts “y is $y”

     }

     Set x 1

     Puts “ x is $x”

     Test

    Puts “x is $x”


Output: x is 1

        Y is 1

    Y is 4

    X is 4

Therefore, initially x is 1 and is printed; but when “test” is called the compilation goes to definition and sees for value of y but it holds the pre-existing value of x into y and only when compilation sees that y is declared with 4 then it sees for printing option and prints again y with value of 4 and this being carried to x and x is declared with value 4.

  1. Global scope:

The variable is called outside the definition.

Syntax: global <variable>

Eg: proc test { } {

Global x

Puts “x is $x”

Proc nested { } {

Global x

Puts “x is $x”

}

  }

Set x 1

Test

Nested

Puts “x is $x”


Output: x is 1

    X is 1

    X is 1


  1. File handling operations:

This is a text processing; each record is converted into a list. We can display or even edit a file.

Commands used are: open, read, close, puts, gets

Puts: to display the content

Puts <user defined channel id> <variable/content>

Gets: it converts records into list and prints the number of records.

Syntax: gets <user defined channel id> <variable>

Open: it opens the file

Syntax: open “<file name”> <access mode>

Close: it closes the file

Syntax: close $<used defined channel id>

Read: to read the content of file

Syntax: read <used defined channel id>

Access modes:

  1. r: It helps in accessing the file in read mode.



  1. w: It is used to write the content into the file, it creates a file if it does not exists, if exists it over writes the content.










  1. a: It is used to append content to the file, the file should exist.



  1. r+: It is used open the file in read mode and even helps in writing content in the file. The file should be existing.


  1. w+: It is used to write and read the file. If file does not exist it creates the file and overwrites the data. 

  2. a+: It is used to append the content on to the file and open in read mode. It creates the file if it does not exist.


  1. Regular expressions:

It is useful when listing operations during search and replace.

regexp: It is used for matching patterns in a string.

regsub:  It is used to replace, search and modify a file.

  1. regexp:

syntax: regexp <options> “<pattern match>” “<string to match>” variable

output of regexp will be the numeric value of how many matches are found.

The most used options are : -inline, -all, -not.


  1. regsub:

syntax: regsub <option> “<pattern match>” “<string to match>” “<string to replace>” <variable>

output of regsub is the numeric value of how many replacements have occurred.

Example on both regexp and regsub:

Set fh [open “file.txt” r]

Set fp [open “file1.txt” w]

While { [gets $fh line] >= 0} {

If { [regexp –all –inline “pass” $line  a] >= 1 } {

Puts $a

Puts [regsub –all “pass” $line “p” b]

Puts $b

} else {

Puts $fp $b

}

}

Close $fh

Close $fp


  1. Strings: Strings are same as array or list command.

    1. Creating a string: set {….}

    2. String compare: If two string are same then print 0 if not prints -1 if string occurs 1st in dictionary order if not it prints 1.

Syntax: sring compare “string1” “string2”


  1. String first: It is used to search a word in string and access only the first occurrence.

Syantx: string first “word” “string”

If the word is found it prints 1 or 0, if not -1


  1. Index: It prints me the number of indexes of the string, i.e. the number of characters of a string. It even includes space as one character.

Syntax: string index “string”

  1. Length: The output of string length is similar to gets command. It prints the total length.

Syntax: string length “string”

  1. Match pattern: 

It matches the pattern with respect to the index. If the exact is not matched then it prints -1 if matches it prints 1.

Syntax: string “string1” “string2”

  1. Trim: 

Trims from both sides

Syntax: string trim “string” “trimming word”

To trim left side: trimleft

To trim right side: trimright

  1. Eval: If command is present in a string and to be evaluated then use eval.

Eg: set a “puts hello”

Eval $a

Output: hello

  1. Concatenate: Two strings are joined using concat.

Syntax: concat “string1” “string2”

  1. Range:

This returns the range of characters in string.

Syntax: string range <variable/string>  <index start> <index end>


Comments

Popular posts from this blog

PERL scripting

Introduction to Linux scripting and types of OS

SHORT CHANNEL DEVICE