Basic commands in Tcl
In this part of the Tcl tutorial, we will cover some basic Tcl commands.In the first example, we will mention the
puts
command. The puts command is used to print messages to the console or to other channels like a file. The command has the following syntax: puts ?-nonewline? ?channelId? stringThe
puts
is the command name. Optional parameters are specified between question marks. The -nonewline
switch suppresses the newline character. By default, the command puts a newline to each message. The channelId must be an identifier for an open channel such as the Tcl standard input channel (stdin), the return value from an invocation of open or socket. It defaults to stdout, if not specified. Finally the string is the message to be printed. #!/usr/bin/tclshThe
puts "This is Tcl tutorial"
puts stdout "This is Tcl tutorial"
puts
command prints a message to the console. Both command invocations do the same thing. #!/usr/bin/tclshHere we use the
puts [open messages w] "This is Tcl tutorial"
puts
command to write to a file. We open a file for writing using the open
command. $ cat messagesWe show the contents of the messages file created by the above Tcl script.
This is Tcl tutorial
Greeting a user.
#!/usr/bin/tclshIn this example, we request an input from the user and print the input in a custom greeting.
puts -nonewline "What is your name? "
flush stdout
gets stdin name
puts "Hello $name"
puts -nonewline "What is your name? "The
-nonewline
option suppresses the newline. The prompt remains on the same line. flush stdoutThe output is buffered. To see the output immediately after the command runs, we can use the
flush
command. The stdout
is the standard output. In our case a terminal. It is called a channel id in Tcl. gets stdin nameThe
gets
command reads a line from the standard input. The result is stored in the name variable. puts "Hello $name"Finally, we greet the user.
$ ./name.tclRunning the example.
What is your name? Jan
Hello Jan
The
info
command returns information about the state of the Tcl interpreter. #!/usr/bin/tclshThe info command has several options. We show three of them.
puts [info tclversion]
puts [info host]
puts [info exists var]
puts [info tclversion]Here we print the version of the Tcl interpreter.
puts [info host]This line prints the host name.
puts [info exists var]Finally we check if the variable var is set.
The
set
command is used to create and read variables. The unset
command destroys a variable. #!/usr/bin/tclshAn example showing the
set x 23
puts $x
puts [set x]
unset x
puts [info exists x]
set
and unset
commands. set x 23We create an x variable and assign a value 23 to it.
puts $xWe print the value of the x variable.
puts [set x]This line also prints the value of the x variable. The
set
command with one parameter reads the value of the variable. The value is passed to the puts
command and printed to the terminal. unset xThe variable x is destroyed.
puts [info exists x]We verify the existence of the variable using the
info exists
command. Tcl scripts like any other scripts can take command line arguments. Tcl has three predefined variables.
- $argc - the number of arguments passed to the script
- $argv - the list of arguments
- $argv0 - the name of the script
#!/usr/bin/tclshWe use all the predefined variables in this script.
puts "The script has $argc arguments"
puts "The list of arguments: $argv"
puts "The name of the script is $argv0"
$ ./args.tcl 1 2 3Running the example.
The script has 3 arguments
The list of arguments: 1 2 3
The name of the script is ./args.tcl
This chapter covered some basics of the Tcl language.
0 comments:
Post a Comment