Posts

Showing posts from October, 2026

Looping In TCL

 In  Conditionals In TCL , we have learnt about the if command and the switch command. In this post we will learn about the while command, the for command and the incr command. Here we are going to write a file named looping.tcl and then we will run it as, tclsh looping.tcl  set cnt 10 puts "Descending order"; while {$cnt > 0} { puts "cnt = $cnt"; set cnt [expr {$cnt - 1}]; } puts "Even numbers"; while {$cnt < 11} { if {$cnt % 2 == 0} { puts "cnt = $cnt"; } # set cnt [expr {$cnt + 1}]; set cnt [incr cnt]; } puts "Odd numbers"; while {$cnt > 0} { set cnt [incr cnt -1]; if {$cnt % 2 == 0} continue; puts "cnt = $cnt"; if {$cnt < 3} break; } for {set idx 1} {$idx < 11} {incr idx} { if {$idx % 2 == 0} continue; puts "idx = $idx"; if {$idx > 8} break; } Happy reading and thank you. Meet you soon.

Conditionals In TCL

 A system can be in a certain condition out of several conditions. To find that condition, we use an if command or a switch command. Firstly, we will discuss about the if command and secondly we will discuss about  the switch command. As we have read in  expr command In TCL  that there is a security risk involved when we use,"" to pass arguments to a command in place of {}. This is also true for if command and others. The if Command Here we are going to write a file named ifcmd.tcl and then we will run it as, tclsh ifcmd.tcl  set x 10; if {$x < 11} { puts "$x is less than 11."; } if {$x < 11 } { puts "$x is less than 11."; } elseif {$x > 11} { puts "$x is greater than 11"; } else { puts "x is 11"; } In the first if command, it is being checked  whether the value of x is less than 11 and found true then the value of {$x <11} will be 1 or found false then the value of {$x < 11} will be 0.When false we know that eith...

More About Commands

In  A Few Commands In Bash Shell  , we have learnt about running a few commands. But where does Bash shell find commands? To answer this question, we are going to learn a few concepts about Bash shell. bin Directory Most of the time, a directory which contains commands is named bin, because commands are binary files. There are several bin directories across the system. Examples of bin directories are: /usr/bin, /usr/local/bin, /home/username/bin, /sbin. Here, we replace username with the name of a user and a user can store its personal commands here. /sbin stores commands for the root user. A normal user might need the root user authority to run it. PATH Variable There is a variable named PATH defined in a hidden file .bashrc in the home directory of a user. The names of directories are separated with :(colon) and assigned to this variable. We can see the value of PATH variable using command: echo $PATH .   When we run a command, the first directory is being searched...

A Few Commands In Bash Shell

Image
 In  Basics Of Bash Shell , we have read about users and terminal. In this post, we will learn a few basic commands to explore a system details. Terminal Again Here we see a terminal window. This is on a Ubuntu system. Here we see a $ symbol in the starting. This is a prompt for a normal user. We have used command sudo su - to   switch to   the root user whose prompt starts with #. Now we can again switch to a normal user using exit command. And the prompt returns to $ again. To run a command we type command against the prompt and hit Enter key. The prompt returns after the command execution and display of the result. At last, we can type exit and end the current session. A Few Commands We use ls command to list files and directories inside the current directory. pwd command is being used to know the path of the current directory. And, we can use a path after cd command to change the current directory e.g. cd /dir1/dir2 or cd ../dir1 . We have learnt about an abso...

Basics Of Bash Shell

 We have read in  Bash Shell In Linux  about a command interpreter known as bash shell. Terminal And Prompt We need a software where we can type our command to be executed. This piece of  software is called a terminal. A prompt is a place where we type our commands. It is usually represented by $ for a normal user and by # for the root user or the admin of the system. Users One who uses a system is known as a user of the system. There are two types of users, a normal user and a root user. A normal user usually has authority over his/her stuff inside the home directory. It means a normal user has no authority over the whole system. There is only one root user on a system. And the root user has absolute authority over the whole system.  A normal user has a directory inside /home directory which is named as user's name. A user joe has a home directory /home/joe. But the root user home directory is /root. No other user can access it. There is a file /etc/passwd whic...

Bash Shell In Linux

Introduction I would always refer to Ubuntu for Linux. Bash shell is an interpreter which we can use to run commands interactively or a script. When we run commands interactively, we enter a command at a time and wait for the result of the command. When we write several commands in a file with extension .sh which is called a script. When we run a script, all commands inside the script is being executed sequentially. Compiler Vs Interpreter Unlike a compiler which converts the text format of a full program into the machine's binary format, an interpreter reads a single line at a time and executes it. In the case of compiler we get the compiled program and sometimes also some other intermediate files but in the case of interpreter we don't get any file. File System A file contains a bunch of data and resides inside a directory which contains several files and directories. As in a tree, a leaf is at the end of a branch and there is a hierarchy of branches, a file is inside a direc...

expr command In TCL

 Until now, we have learnt to assign a value to  a variable using the set command. But if we'd like to add, subtract or to perform some other types of math operations then we can use expr command to perform such operations.  We have read in  Using A Value Returned By A Command In TCL  to capture the returned value by a command. So we can use expr command inside [] and use the value returned by expr command.  Before seeing expr in action, we have read in  Grouping Strings In TCL  that a variable value is not substituted inside {}. So, to stop TCL interpreter to substitute the value of a variable before expr command has an opportunity to evaluate it, we often enclose arguments to expr inside {} as a security measure. Here we are going to write a file named exprp1.tcl and then we will run it as, tclsh exprp1.tcl  set a 20; set b 020; # It's an octal value and its value is 16. puts [expr {$a + $b}]; # 36 puts [expr {$a - $b}]; # 4 In the above ex...

Using A Value Returned By A Command In TCL

 In  Variables In TCL , we were unable to see the value returned by a set command. To capture the value returned by a command, we enclose that command inside []. In  Grouping Strings In TCL , we have read about groping using "" and {}. Here we are going to write a file named return.tcl and then we will run it as, tclsh return.tcl puts [set pi 3.14]; puts [set pi]; puts [puts "A string"]; puts "[set x 5]"; puts {[set y 3]}; puts "\[set z 6\]";  In the first line, we are setting a value 3.14 to a variable pi and displaying the value returned by the set command. In the second line, we are just displaying the value returned by the set command.  Next, 'A string' is printed and cursor moves to the next line but puts doesn't return any value, so nothing is being displayed. Command execution using [] works when enclosed inside "" but not {}. So we see 5 and [set y 3]. In the last line, [ has been escaped, so set command is not execu...

Grouping Strings In TCL

 In  Variables In TCL , how to assign a value to a variable using set command. In this post we are going to discuss about the grouping of strings as a single argument. Arguments are data provided to a command. If we don't group them, they will be treated as several data and command will not perform its task as it is expected. We can group multiple string/word as a single argument using "" and {}. But, they behave differently. In  Variables In TCL , we have read that when we prefix $ with a variable, we read the value of that variable. It's true in the case of grouping with "" but not in the case of {}. Here we are going to write a file named grouping.tcl and then we will run it as, tclsh grouping.tcl  # Assigning "circle" to shape variable. set shape "circle"; # Displaying the shape value puts "The shape is: $shape"; puts {The shape is: $shape}; # It will not display the shape value. puts "The shape is: {$shape}"; # Bu...

Variables In TCL

 Variable is a lifeline for a programming language. It provides a mechanism to a programming language to name a portion of a memory and reference a data stored there.  We use assignment operation to set a particular value to a variable. In TCL, we use set command for assignment. we can use set command in two ways. set pi 3.14;   # assigning a value (case 1) set pi;  # reading a variable (case 2) In Hello TCL , we get introduced to TCL language. In case 1, a variable named pi is being assigned a value 3.14. We can also assign a string to a variable. After assigning a value to a variable, set command returns the value. In this example, 3.14. In case 2, set command merely returns the value assigned to this variable. In this example, 3.14. Another way to read the value of a variable is prefixing the variable with $ e.g. $pi. Here is an example, puts $pi Here we are going to write a file named variable.tcl and then we will run it as, tclsh variable.tcl  # Assign...

Hello TCL

TCL is an interpreter based language. It is mainly used in VLSI design. tclsh is an interpreter for this language. We can directly run a command using tclsh in interactive mode or we can write our command in a file with extension .tcl and run this file using tclsh interpreter.   Here we are going to write a file named hello.tcl and then we will run it as, tclsh hello.tcl   # This is a comment. It is being used to help the readers of this code.  # It is not executed by tclsh interpreter i.e. interpreter ignores it.  # It is effective until the end of the current line.  # It can be written after a command ending with a semicolon.  # puts is being used to write a string on the console.  # We can use double quotes or curly braces to write a string using several words.  # We can write a command without ending with a semicolon but we can't write a comment after it.   puts HelloTCL!  puts "Hello TCL!"; # A string with multiple wo...

Creating An Animation Using A Text Editor

 Most of time, when we think of an image or an animation, we force ourselves  to think about binary data and a bulky IDE to create them. Again, there is a trap associated with pixel-oriented images and animations, when it is being zoomed in, they starts to become pixelated.  There is a text-oriented image format known as Scalar Vector Graphics(SVG). Basically, SVG image contains commands regarding drawing of an image rather encoding every pixel of the image using a set of binary bits. We can create an SVG outright here in this blog. In this image we have created a rectangle as a background and drawn a circle on top of it. Happy reading and thank you. Meet you soon.