You are on page 1of 14

ex1.

tcl ==========================================================
#to enter the tcl interpreter use command `tclsh`
#in tcl interpreter we can print the tcl version using the command
#puts [$tcl_version]
#to get the tcl interpreter path `info library`
#to exit use `quit`

=================================================================
ex2.tcl ==========================================================
proc increment { val } {
set res {}
foreach i $val {
if { [ string is integer $i ]} {
incr i
lappend res $i
} else {
scan $i %c asci
incr asci
lappend res [format %c $asci]
}
}
puts $res
}
puts "enter the list :::::"
flush stdout
set str [ gets stdin ]
puts -nonewline [ increment $str ]
=================================================================
ex3.tcl ==========================================================
#!/usr/share/tcl8.6
set str "ccccccccaaabbbbaaaabbinformationabcaaaaaabbbbbbbccbb"
puts [regexp "information" $str res]
puts $res
=================================================================
ex4.tcl ==========================================================
set lst { 1 2 3 4 5 }
set joined_list [join $lst ""]
puts $joined_list
set str "12345"
set seperated_str [split $str ""]
puts $seperated_str
=================================================================
ex5.tcl ==========================================================
#program to print a pattern
puts "enter a number:::::"
set n [gets stdin]
puts $n
set decr 2
for { set i 0 } { $i<=[expr 2*$n] } { incr i } {
if { $i>$n } {
set k [ expr $i-$decr ]
incr decr 2
} else {
set k $i
}
for { set j 0 } { $j<=$k } { incr j } {
puts -nonewline "$j "
}
puts ""
}
=================================================================
ex6.tcl ==========================================================
#print the pattern
# *
# * *
# * * *
# * * * *
puts "Enter a number :::::::::"
set n [ gets stdin ]
for { set i 0 } { $i<=$n } { incr i } {
for { set j 0 } { $j<=$i } { incr j } {
puts -nonewline " * "
}
puts ""
}
=================================================================
ex7.tcl ==========================================================
set str { \
Input:
sysDescr.0 = STRING: "SunOS zeus.net.cmu.edu 4.1.3_U1 1 sun4m"
ifIndex.1 = INTEGER: 1
ifIndex.2 = INTEGER: 2
ifDescr.1 = STRING: "lo0"
}
set str2 {
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (51217510) 5 days, 22:16:15.10
SNMPv2-MIB::sysContact.0 = STRING:
SNMPv2-MIB::sysName.0 = STRING: Switch
SNMPv2-MIB::sysLocation.0 = STRING:
SNMPv2-MIB::sysServices.0 = INTEGER: 78
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORID.1 = OID: SNMPv2-SMI::enterprises.9.7.129
SNMPv2-MIB::sysORID.2 = OID: SNMPv2-SMI::enterprises.9.7.115
SNMPv2-MIB::sysORID.3 = OID: SNMPv2-SMI::enterprises.9.7.265
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-SMI::enterprises.9.7.112
SNMPv2-MIB::sysORID.5 = OID: SNMPv2-SMI::enterprises.9.7.106
SNMPv2-MIB::sysORID.6 = OID: SNMPv2-SMI::enterprises.9.7.47
SNMPv2-MIB::sysORID.7 = OID: SNMPv2-SMI::enterprises.9.7.122
SNMPv2-MIB::sysORID.8 = OID: SNMPv2-SMI::enterprises.9.7.135
SNMPv2-MIB::sysORID.9 = OID: SNMPv2-SMI::enterprises.9.7.37
SNMPv2-MIB::sysORID.10 = OID: SNMPv2-SMI::enterprises.9.7.92
SNMPv2-MIB::sysORID.11 = OID: SNMPv2-SMI::enterprises.9.7.53
SNMPv2-MIB::sysORID.12 = OID: SNMPv2-SMI::enterprises.9.7.54
SNMPv2-MIB::sysORID.13 = OID: SNMPv2-SMI::enterprises.9.7.52
SNMPv2-MIB::sysORID.14 = OID: SNMPv2-SMI::enterprises.9.7.93
SNMPv2-MIB::sysORID.15 = OID: SNMPv2-SMI::enterprises.9.7.186
SNMPv2-MIB::sysORID.16 = OID: SNMPv2-SMI::enterprises.9.7.128
SNMPv2-MIB::sysORID.17 = OID: SNMPv2-SMI::enterprises.9.7.121
SNMPv2-MIB::sysORID.18 = OID: SNMPv2-SMI::enterprises.9.7.44
SNMPv2-MIB::sysORID.19 = OID: SNMPv2-SMI::enterprises.9.7.350
SNMPv2-MIB::sysORID.20 = OID: SNMPv2-SMI::enterprises.9.7.33
SNMPv2-MIB::sysORID.21 = OID: SNMPv2-SMI::enterprises.9.7.130
SNMPv2-MIB::sysORID.22 = OID: SNMPv2-SMI::enterprises.9.7.568
SNMPv2-MIB::sysORID.23 = OID: SNMPv2-SMI::enterprises.9.7.116
SNMPv2-MIB::sysORID.24 = OID: SNMPv2-SMI::enterprises.9.7.91
SNMPv2-MIB::sysORID.25 = OID: SNMPv2-SMI::enterprises.9.7.999
SNMPv2-MIB::sysORID.26 = OID: SNMPv2-SMI::enterprises.9.7.9999
SNMPv2-MIB::sysORID.27 = OID: SNMPv2-SMI::enterprises.9.7.126
SNMPv2-MIB::sysORID.28 = OID: SNMPv2-SMI::enterprises.9.7.127
SNMPv2-MIB::sysORID.29 = OID: SNMPv2-SMI::enterprises.9.7.64
SNMPv2-MIB::sysORID.30 = OID: SNMPv2-SMI::enterprises.9.7.439
SNMPv2-MIB::sysORID.31 = OID: SNMPv2-SMI::enterprises.9.7.438
SNMPv2-MIB::sysORID.32 = OID: SNMPv2-SMI::enterprises.9.7.123
SNMPv2-MIB::sysORID.33 = OID: SNMPv2-SMI::enterprises.9.7.120
SNMPv2-MIB::sysORID.34 = OID: SNMPv2-SMI::enterprises.9.7.124
SNMPv2-MIB::sysORID.35 = OID: SNMPv2-SMI::enterprises.9.7.119
SNMPv2-MIB::sysORID.36 = OID: SNMPv2-SMI::enterprises.9.7.125
SNMPv2-MIB::sysORID.37 = OID: SNMPv2-SMI::enterprises.9.7.45
SNMPv2-MIB::sysORID.38 = OID: SNMPv2-SMI::enterprises.9.7.445
SNMPv2-MIB::sysORID.39 = OID: SNMPv2-SMI::enterprises.9.7.595
SNMPv2-MIB::sysORID.40 = OID: SNMPv2-SMI::enterprises.9.7.46
SNMPv2-MIB::sysORID.41 = OID: SNMPv2-SMI::enterprises.9.7.584
SNMPv2-MIB::sysORID.42 = OID: SNMPv2-SMI::enterprises.9.7.201
SNMPv2-MIB::sysORID.43 = OID: SNMPv2-SMI::enterprises.9.7.202
SNMPv2-MIB::sysORID.44 = OID: SNMPv2-SMI::enterprises.9.7.440
SNMPv2-MIB::sysORID.45 = OID: SNMPv2-SMI::enterprises.9.7.16
SNMPv2-MIB::sysORID.46 = OID: SNMPv2-SMI::enterprises.9.7.17
SNMPv2-MIB::sysORID.47 = OID: SNMPv2-SMI::enterprises.9.7.319
SNMPv2-MIB::sysORID.48 = OID: SNMPv2-SMI::enterprises.9.7.36
SNMPv2-MIB::sysORID.49 = OID: SNMPv2-SMI::enterprises.9.7.209
SNMPv2-MIB::sysORID.50 = OID: SNMPv2-SMI::enterprises.9.7.412
SNMPv2-MIB::sysORID.51 = OID: SNMPv2-SMI::enterprises.9.7.406
SNMPv2-MIB::sysORID.52 = OID: SNMPv2-SMI::enterprises.9.7.256
SNMPv2-MIB::sysORID.53 = OID: SNMPv2-SMI::enterprises.9.7.101
SNMPv2-MIB::sysORID.54 = OID: SNMPv2-SMI::enterprises.9.7.105
SNMPv2-MIB::sysORID.55 = OID: SNMPv2-SMI::enterprises.9.7.108
SNMPv2-MIB::sysORID.56 = OID: SNMPv2-SMI::enterprises.9.7.103

}
set res {}
set exp {[A-Za-z0-9.]+ = [A-Z]+: ([^\n]+)}
foreach i [split $str2 "\n"] {
if { [regexp $exp $i mtch grp] } {
lappend res $grp
}
}
foreach i $res {
puts $i
}
=================================================================
ex8.tcl ==========================================================
puts "Enter a string::::::"
set str [ gets stdin ]
set exp {(\w+) (\w+) \"(\w+.\w+)\"}
puts [regexp $exp $str mtch grp1 grp2 grp3]
puts $grp3
=================================================================
ex9.tcl ==========================================================
puts "enter a directory in current directory::"
set dir [gets stdin]
set cur [pwd]
set wrk [file join $cur $dir]
if { [file isdirectory $wrk] } {
set file_list [glob -nocomplain $wrk/*]
foreach fpath $file_list {
puts "$fpath-#########################"
set fp [open $fpath r+]
puts [read $fp]
puts "#########################"
}
} else {
puts "there is no directory"
}
=================================================================
ex10.tcl ==========================================================
set str "hi hello"
#count of characters without spaces
puts [regexp -all {[^ ]} $str]
#count of characters with space
puts [regexp -all "" $str]
set count 0
foreach i [split $str ""] {
incr count
}
=================================================================
ex11.tcl ==========================================================
#we can extract a single letter from a string using regex
set str {Test TCL Tool TransactionId TPID Tag Tftp}
puts [regexp "t" $str mtch]
puts $mtch
=================================================================
ex12.tcl ==========================================================
set days [list "Monday" "Tuesday" "Wednessday" "Thursday" "Friday" "Saturday"
"Sunday"]
foreach i $days {
puts "first :[string index $i 0] last : [string index $i end]"

}
=================================================================
ex13.tcl ==========================================================
puts "enter IP to go to next ip:"
set adr [gets stdin]
set exp4 {^([0-9]{0,3}).([0-9]{0,3}).([0-9]{0,3}).([0-9]{0,3})$}
set exp6 {^([0-9a-fA-F]{0,4}):([0-9a-fA-F]{0,4}):([0-9a-fA-F]{0,4}):([0-9a-fA-F]
{0,4}):([0-9a-fA-F]{0,4}):([0-9a-fA-F]{0,4}):([0-9a-fA-F]{0,4}):([0-9a-fA-F]{0,4})
$}
if {[regexp $exp4 $adr mtch ip1 ip2 ip3 ip4]} {
set lst [list $ip1 $ip2 $ip3 $ip4]
set flag 0
set inp_flag 1
foreach v $lst {
if {$v>255} {
set inp_flag 0
}
}
for { set i 3 } { $i>0 && $inp_flag == 1 } { incr i -1} {
if {[expr [lindex $lst $i]+1 ] <= 255} {
lset lst $i [expr [lindex $lst $i]+1 ]
set flag 1
break
} else {
lset lst $i 0
}
}
if {$flag == 1 && $inp_flag == 1} {
puts "ip incremented successfully"
puts "next ip : [join $lst "."]"
} else {
puts "cannot add ip "
}
} elseif {[regexp $exp6 $adr mtch ip1 ip2 ip3 ip4 ip5 ip6 ip7 ip8]} {
set lst [list $ip1 $ip2 $ip3 $ip4 $ip5 $ip6 $ip7 $ip8]
set flag 0
for { set i 7 } { $i>0 } { incr i -1} {
set hx "0x[lindex $lst $i]"
set hx [incr hx]
if {$hx <= 65535 } {
lset lst $i [format "%04x" $hx]
set flag 1
break
} else {
lset lst $i [format "%04x" 0]
}
}
if {$flag == 1} {
puts "ip incremented successfully"
puts "next ip : [join $lst ":"]"
} else {
puts "cannot add ip "
}
} else {
puts "ip invalid"
}

=================================================================
ex14.tcl ==========================================================

# 15.Write a TCL script to convert following:


# 1.{one two three four} -> one.two.three.four
# 2. /usr/cisco/bin/tclsh -> usr cisco bin tclsh
# 3. Juniper -> J U N I P E R
# 4. {B R O A D C O M} -> Broadcam

puts "enter the input::"


gets stdin inp
set res ""
if {[regexp "/.+" $inp mtch]} {
puts "1"
set res [ regsub -all "/" $inp { } ]

} elseif {[regexp {\{(\w .+)\}} $inp mtch grp]} {


puts "2"
set lst {}
for {set i 0} {$i<[llength $grp]} {incr i} {
if {$i==0} {
lappend lst [string toupper [lindex $grp $i]]
} else {
lappend lst [string tolower [lindex $grp $i]]
}
}
set res [join $lst ""]
} elseif {[regexp {\{(\w{2,} .+)\}} $inp mtch grp]} {
puts 3
set res [join [split $grp " "] "."]

} elseif { [regexp {\w+} $inp mtch ] } {


puts 4
set lst {}
foreach i [split $inp ""] {
lappend lst [string toupper $i]
}
set res [join $lst ""]
}
puts $res

=================================================================
ex15.tcl ==========================================================
set str {facetious abstemious rhythm myth}
set voe_count 0
foreach i $str {
foreach j [split $i ""] {
if { $j in { a e i o u A E I O U } } {
incr voe_count
}
}
}
puts "vowel count is $voe_count"
=================================================================
ex16.tcl ==========================================================
set list1 {1 2 3}
set list2 {a b c}
set res_list {}
set n_list [concat $list1 $list2 ]
foreach i $list1 {
lappend res_list $i
}
foreach i $list2 {
lappend res_list $i
}
puts $res_list
puts $n_list
=================================================================
ex17.tcl ==========================================================
puts "enter a list of elements ::::"
set lst [gets stdin]
puts $lst
puts "enter the element you want to delete ::::::"
set del [gets stdin]
puts $del
for { set i 0 } {$i<[llength $lst]} {incr i} {
set val [lindex $lst $i]
if {$val == $del} {
set lst [lreplace $lst $i $i]
}
}
puts $lst
=================================================================
ex18.tcl ==========================================================
#set list1 {venkat gopi prashanth mahantesh krishna nagendra venkat krishna}
puts "enter the list :::"
set list1 [gets stdin]
set len [llength $list1]
for { set i 0 } { $i<=$len} {incr i} {
for { set j [expr $i+1] } { $j<=$len } {incr j} {
if { [lindex $list1 $i] == [lindex $list1 $j] } {
set list1 [lreplace $list1 $j $j]
}
}
}
puts $list1
=================================================================
ex19.tcl ==========================================================
puts "$tcl_version"
=================================================================
ex20.tcl ==========================================================
set glob_val 1
puts $glob_val
proc test_global {} {
upvar glob_val var
set var 2

}
test_global
puts $glob_val
=================================================================
ex21.tcl ==========================================================
#setting a variable with the value
set value "hi"
puts $value
unset value
#should get a error showing there is no such variable
#because tcl removes the reference and value
catch {puts $value} err
puts $err
=================================================================
ex22.tcl ==========================================================
#invoke another program from tcl
#you can use source command
source "ex5.tcl"
#can use eval command and execute through tclsh command
puts [exec tclsh ex5.tcl]
#executing all the tcl files in the folder
puts $argv0
set i 0
foreach var [glob ex*.tcl] {
if {$var != $argv0} {
#puts [eval exec tclsh $var]
source $var
}
incr i
if {$i>3} {
break
}
}
=================================================================
ex23.tcl ==========================================================
#script to get email from a string
set str {
Write Regexp to match email-id:
j_smith@foo.com
j.smith@bc.canada.ca
smith99@foo.co.uk
1234@mydomain.net
}
set str2 {
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis urna eget
mauris pretium rhoncus sed non tortor. In hac habitasse platea dictumst. Fusce
pellentesque urna sed velit auctor, vel tristique magna sagittis. Quisque bibendum
orci vitae fermentum vehicula. Donec id sem a odio pretium feugiat non a augue.
Maecenas in leo non enim fermentum euismod. Sed dignissim turpis eget lorem
imperdiet, vel maximus orci sagittis.

Here are some example email addresses:


user0@gmail.com
user1@outlook.com
user2@yahoo.com

Sed euismod risus id enim ullamcorper congue. Sed luctus, augue sed bibendum
pretium, dui tellus ultricies est, eu consectetur leo velit id enim. Nam auctor,
nibh vitae hendrerit pellentesque, dolor augue viverra ante, quis varius augue
dolor vitae mi. Ut ac ex at turpis sollicitudin auctor. Fusce gravida augue ex, non
suscipit quam tincidunt in. Integer et augue euismod, ultrices ex vel, efficitur
purus. Phasellus a dui euismod, imperdiet dolor in, consectetur elit. Sed vehicula
diam ac velit pharetra posuere. Sed vel aliquam lorem. Vivamus at ipsum vitae orci
ultricies elementum a quis sapien. Donec nec ipsum vel lacus pulvinar euismod.

}
set expr {[^ ]+@\w+.[^ ]+}
set emails [regexp -line -inline -all $expr $str2]
foreach var $emails {
puts $var
}
=================================================================
ex24.tcl ==========================================================
#array set lang { 0 TCL 1 Ruby 2 Perl 3 C-language }
#puts $lang(0)
#creating an array with index as keys
puts "enter the input comma seperated ::"
gets stdin inp
set lst [split $inp ","]
array set arr {}
for {set i 0} {$i<[llength $lst]} {incr i} {
set arr($i) [lindex $lst $i ]
puts $arr($i)
}
#creating a associative array with manual key value assignments
dict set code {IPSEC VPN count} ".1.3.6.1.4.1.9.9.171.1.3.1.1.0."
dict set code {SSLVPN count (Anyconnect)} ".1.3.6.1.4.1.9.9.392.1.3.35.0."
dict set code {WEBVPN count} ".1.3.6.1.4.1.9.9.392.1.3.38.0."
dict set code {IKE Active Tunnel} ".1.3.6.1.4.1.9.9.171.1.2.1.1.0."
dict for { key value } $code {
puts "$key :: $value"
}
puts ""
puts "enter the key you want to check::"
gets stdin ava_key
if {[dict exists $code $ava_key ]} {
puts "key is present and value for that key is [dict get $code $ava_key]"
} else {
puts "key is not present"
}

=================================================================
ex25.tcl ==========================================================
puts "enter the list with comma seperated :::"
gets stdin inp
set lst [split $inp ","]
puts "enter the element to be searched::"
gets stdin key
set ind [lsearch $lst $key]
if {$ind!=-1} {
puts [lindex $lst $ind ]
incr ind
puts [lindex $lst $ind ]
incr ind
puts [lindex $lst $ind ]
} else {
puts "vlan_id not found"
}

=================================================================
ex26.tcl ==========================================================
#ternary conditional operator
set a 100
set b 1000
puts [expr {$a>$b ? $a:$b}]
=================================================================
ex27.tcl ==========================================================
proc calculator { oper a b} {
switch $oper {
add {
return [expr $a+$b]
}
sub {
return [expr $a-$b]
}
mul {
return[expr $a*$b]
}
div {
return [expr $a/$b]
}
exp {
return [expr $a**$b]
}
default {
return "enter valid operation"
}
}
}
puts "enter the command and arguments ::"
gets stdin inp
set inp [split $inp " "]
puts [eval $inp]
puts [calculator [lindex $inp 0] [lindex $inp 1] [lindex $inp 2] ]
=================================================================
ex28.tcl ==========================================================
#using string function
puts "enter a number upto which palindrome ::"
gets stdin inp
set res {}
for { set i 1 } { $i<=$inp} {incr i} {
if {$i == [string reverse $i]} {
puts $i
}
}
=================================================================
ex29.tcl ==========================================================

=================================================================
ex30.tcl ==========================================================
#Write a TCL script to do following:
# Input -- output
# 1.100.5 -- 100.500000
# 2.43.5 -- 4.350000e+01
# 3.4 tuts -- 4 tuts
# 4.TCL Lang -- TCL language
# 5.40 -- Corresponding Hexadecimal value
# 6.50 -- Corresponding binary format

set n 100.5
puts [format "%.6f" $n]
set n1 43.5
puts [format "%.6e" $n1]
set n2 "tuts"
puts "[string length $n2] $n2"
set n3 "TCL Lang"
set res [ regsub "Lang" $n3 "Language" ]
puts $res
set n4 40
puts [format "%x" $n4]
set n5 50
puts [format "%b" $n5]
=================================================================
ex31.tcl ==========================================================
#break statement gets out of the entire loop
set i 10
for {set j 0} {$j<=15} {incr j} {
if {$i ==$j} {
break
}
puts $j
}
#continue will jump to next iteration
for {set j 0} {$j<=15} {incr j} {
if {$i ==$j} {
continue
}
puts $j
}
=================================================================
ex32.tcl ==========================================================
set lst [list RSTP VLAN LACP ]
puts $lst
lappend lst TCL
puts $lst
set lst [linsert $lst 0 Ruby]
puts $lst
set lst [linsert $lst [lsearch $lst VLAN] python]
puts $lst
foreach i $lst {
puts "length is [string length $i] and value is $i"
}
lset lst [lsearch $lst RSTP] MSTP
puts $lst
set proto1 [lindex $lst [lsearch $lst Ruby]]
set proto2 [lindex $lst [lsearch $lst LACP]]
puts $proto1
puts $proto2
puts [lsort $lst]
puts [lsort -decreasing $lst]
=================================================================
ex33.tcl ==========================================================
dict set rstp_cmds {configMode} {configure}
dict set rstp_cmds {interfaceMode} {interface 1/0/1}
#dict set rstp_cmds {setPortfast} {spanning-tree portfast}
#dict set rstp_cmds {showSpannTree} { show spanning-tree }
dict set rstp_cmds {showRunnConfig} {show running-config interface
tengigabitethernet 1/0/1}
puts [dict size $rstp_cmds]
set keys [dict keys $rstp_cmds]
set val [dict values $rstp_cmds]
puts $keys
puts $val
if {![expr { "spanning-tree portfast" in $val }]} {
dict set rstp_cmds {setPortfast} {spanning-tree portfast}
}
if {![expr { "show spanning-tree" in $val }]} {
dict set rstp_cmds {showSpannTree} { show spanning-tree }
}
dict get $rstp_cmds "showSpannTree"
dict for {i j} $rstp_cmds {
puts "$i:$j"
}
puts "enter key to search:"
gets stdin src
if {$src in [dict keys $rstp_cmds]} {
puts "key is present"
} else {
puts "key not present"
}

=================================================================
ex34.tcl ==========================================================
puts "enter the numbers ::"
gets stdin inp
if {[regexp "," $inp]} {
set lst [split $inp ","]
} else {
set lst [split $inp " "]
}
set n [llength $lst]
set sum 0
foreach i $lst {
incr sum $i
}
puts "average is [expr "$sum/$n"]"
=================================================================
ex35.tcl ==========================================================
#create vlans upto 4046
dict set vlans {1} {vlan1}
for { set i 2 } { $i<=4046 } { incr i } {
dict set vlans $i "vlan $i"
}
dict for { id val } $vlans {
puts "$id:$val"
}
=================================================================
ex36.tcl ==========================================================
set fp [ open "testfileWrite.txt" "w" ]
set str {\
R2#show arp
Protocol Address Age (min) Hardware Addr Type Interface
Internet 1.1.1.1 23 0000.0000.0001 ARPA FastEthernet0/0
Internet 1.1.1.2 - 0000.0000.0002 ARPA FastEthernet1/2
R2#show mac
R2#show mac-address-table
R2#
}
puts -nonewline $fp $str
=================================================================
ex37.tcl ==========================================================
set fp [ open "testfileWrite.txt" r+ ]
set str "Internet 20.20.20.10 30 - 0A63.80FE.CF01 ARPA Ethernet1/10"
set inp [read $fp]
set lst [split $inp "\n"]
set dest_line [lsearch $lst "Internet 1.1.1.2 - 0000.0000.0002
ARPA FastEthernet1/2"]
for {set i 0} {$i<[llength $lst]} {incr i} {
if {$i==$dest_line } {
set lst [linsert $lst $i $str]
}
}
close $fp
set fp [open "testfileWrite.txt" w]
puts $fp [join $lst "\n"]

=================================================================
ex38.tcl ==========================================================
set fp [open "mac_addr.txt" r]
set fp1 [open "mac_addr2.txt" r]
set stat [open "staticentr.txt" w]
set dyn [open "dynentr.txt" w]
puts $stat ""
puts $stat {\
Static entries
--------------
Vlan Mac Address Type Ports Moves Last Move
---- ----------- ---- ----- ----- ---------
}
puts $dyn {\
Dynamic entries
--------------
Vlan Mac Address Type Ports Moves Last Move
---- ----------- ---- ----- ----- ---------
}
set stat_count 0
set dyn_count 0
foreach i [list $fp $fp1] {
while {[gets $i mtch] !=-1} {
if {[ regexp "STATIC" $mtch line]} {
incr stat_count
puts $stat $mtch
} elseif {[ regexp "DYNAMIC" $mtch line]} {
incr dyn_count
puts $dyn $mtch
}
}
}
puts $stat "\nnumber of static entries :: $stat_count"
puts $dyn "\nnumber of dynamic entries :: $dyn_count"
close $fp
close $fp1
close $stat
close $dyn

=================================================================
ex39.tcl ==========================================================
catch {[open "notthere.txt" r]} errormsg
puts "error handled::"
puts $errormsg`
=================================================================
ex40.tcl ==========================================================
proc division {a b} {
catch {puts [expr $a/$b]} error
return $error
}
while {1} {
puts "enter a number ::"
gets stdin a
puts "enter a number ::"
gets stdin b
if {[division $a $b]==""} {
break
} else {
puts "cannot [division $a $b]"
puts "reenter correct values"
}

}
=================================================================
ex41.tcl ==========================================================
#script to input error handling
while {1} {
puts "enter a number:"
gets stdin a
puts "enter a number :"
gets stdin b
if {[catch {expr $a*$b} result] != 0} {
puts "invalid input"

} else {
puts "product of $a and $b is [expr $a*$b]"
break
}
}
=================================================================
ex42.tcl ==========================================================
set clk [clock format [clock seconds] -format "%H:%M:%S %d/%m/%Y "]
puts $clk
set time24 [clock format [clock seconds] -format "%H:%M:%S"]
puts $time24
set time12 [clock format [clock seconds] -format "%I:%M:%S %p"]
puts $time12
set timez [clock format [clock seconds] -format "%H:%M:%S %Z"]
puts $timez
set timez [clock format [clock seconds] -gmt 1 -format "%H:%M:%S %Z"]
puts $timez
=================================================================
ex43.tcl ==========================================================
#executing commands
set start [clock milliseconds]
catch {exec mkdir testdir} msg
exec touch testdir/testfile.txt
set name "hello"
set fp [open "testdir/testfile.txt" w]
set cmds {date ps {ifconfig lo} history}
foreach cm $cmds {
puts $cm
set out [eval exec bash -c $cm]
puts $fp $out
}
after 1000
set end [clock milliseconds]
set dur [expr $end-$start]
puts $fp "\nDuration time in milliseconds :::: $dur"
close $fp

=================================================================
expr_creator.tcl================================================
set lst {}
set fp [open "expr_results.txt" w]
for {set i 1} {$i<=43} {incr i} {
lappend lst "ex$i.tcl"
}
foreach file $lst {

set rd [open "$file" r]


puts $file
set code [read $rd]
puts $fp "$file =========================================================="
puts $fp $code

puts $fp "================================================================="

close $fp
close $rd
===============================================================================

You might also like