Skip to main content

Tcl/Tk Programming Basics | NS2 Basics | Lecture 3

Tcl/Tk Programming Basics

In this chapter, you'll learn:

  • ✅ The basics of Tcl/Tk Programming
  • ✅ How to create and execute a Tcl script

📘 Introduction to Tcl Programming

Over the past 25+ years, Tcl (Tool Command Language) has evolved through contributions from industry, academia, and open-source communities. It remains a popular scripting language in areas like:

  • 🔧 Network equipment testing (e.g., routers, switches)
  • 📐 Electronic IC design
  • 🧪 Network Simulator 2 (NS2) scripting and simulation

Created by John Ousterhout, Tcl was designed for embedded systems, offering:

  • ⚡ Rapid prototyping
  • 📦 Small memory footprint
  • 📝 Versatile scripting abilities

✨ Key Features of Tcl/Tk

  • Rapid Development: Tcl performs 5–10× faster in GUI, string handling, and integration tasks compared to other scripting languages.
  • GUI Support: With the Tk toolkit, creating graphical interfaces is easy and powerful.
  • Cross-Platform: Tcl/Tk works seamlessly on Windows, macOS, and Linux.
  • Continuous Upgrades: Regular updates since the 1990s.
  • Easy Deployment: Tcl scripts can be bundled as a single application.
  • Network Capable: Excellent event-driven capabilities for networking tasks.
  • Vibrant Community: Free plugins, extensions, and open source tools.

🧑‍💻 Tcl Interpreter and Script Execution

Tcl scripts usually have the .tcl extension. You can run them using tclsh or wish.

# On Linux/Unix
tclsh filename.tcl

# On Windows
tclsh86.exe filename.tcl
source filename.tcl
  • tclsh: Command-line shell
  • wish: Tcl interpreter with GUI support
TCL
TCL


🚀 Getting Started with Tcl Syntax

🖨️ Example 1: Hello World

# This is an example to demonstrate Hello World
puts "Hello World!"  ;# Print to the screen

🧾 Basic Tcl Syntax

Commands in Tcl are separated by newlines. Example:

expr 3 + 10

Returns 13. Tcl treats everything as a string by default.


📌 Assigning Variables

set Name "I am Tcl"
set A 10
puts $A
puts $Name

set label "The value in A is: "
puts "$label $A"

🔁 Command Substitution

set a 25
set b [expr $a * 4]

Here, b becomes 100.


🔤 Quotes and Braces

set x 10
set y 20
set z "$x + $y is [expr $x + $y]"

Value of z: 10 + 20 is 30

set z {$x + $y is [expr $x + $y]}

Braces prevent substitution, storing literal text.


🔄 Loops and Control Structures

✅ if...elseif...else
puts "What’s your mark in Scripting Language?"
gets stdin marks

if {$marks > 90} {
    puts "Great, you got S Grade"
} elseif {$marks > 50 && $marks < 90} {
    puts "Hey You passed the Exam"
} else {
    puts "You need to improve your marks next time"
}
🔁 for Loop – Sum of N Numbers
set x 10
set sum 0

for {set i 0} {$i <= $x} {incr i} {
    set sum [expr $sum + $i]
}

puts $sum  ;# Output: 55
🔂 while Loop – Squares
set x 0
while {$x < 10} {
    puts [expr $x * $x]
    incr x
}

🧩 Procedures

No Arguments
proc display {} {
    set ns [expr 1 + 2]
    puts $ns
}
display
With Arguments
proc hello {f} {
    puts "Hello Mr. $f"
}

hello "Pradeep"
hello "Amit"
Biggest of Three Numbers
proc biggest {a b c} {
    if {$a > $b && $a > $c} {
        return $a
    } elseif {$b > $a && $b > $c} {
        return $b
    } else {
        return $c
    }
}

set a 10
set b 20
set c 30

set val [biggest $a $b $c]
puts "The biggest of $a, $b and $c is $val"

📁 File Handling

✍️ Writing to a File
set nf [open file.txt w]
puts $nf "This is a simple example to write to a file"
close $nf
📎 File Modes
ModeDescription
rRead-only (file must exist)
r+Read and write
wWrite (overwrite or create)
w+Read and write (overwrite or create)
aAppend (create if not exist)
a+Read and append
📌 Append and Close
set tracef [open file.dat a]
puts $tracef "This is an example to show the file close"
close $tracef

🧾 Conclusion

This chapter introduced the essentials of Tcl programming, especially useful for those working on Network Simulator 2 (NS2). Tcl’s simplicity and powerful scripting features make it a perfect fit for simulation tasks.

For deeper knowledge, explore full Tcl/Tk tutorials and documentation available online.

---

Comments

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Simulation of URDF, Gazebo and Rviz | ROS Noetic Tutorial 8

Design a User-defined robot of your choice (or you can use the URDF file) and enable the LIDAR Scanner so that any obstacle placed on the path of the light scan will cut the light rays. Visualize the robot in the Gazebo workspace, and also show the demonstration in RViz.   (NB: Gain knowledge on wiring URDF file and .launch file for enabling any user-defined robot to get launched in the gazebo platform.) SLAM : One of the most popular applications of ROS is SLAM(Simultaneous Localization and Mapping). The objective of the SLAM in mobile robotics is to construct and update the map of an unexplored environment with the help of the available sensors attached to the robot which will be used for exploring. URDF: Unified Robotics Description Format, URDF, is an XML specification used in academia and industry to model multibody systems such as robotic manipulator arms for manufacturing assembly lines and animatronic robots for amusement parks. URDF is especially popular with users of the ...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...