On our Sun Sparc machines, the usual magic number for executables is 0x81 0x03, though there are variations.
The magic number concept is used in Unix to type or
identify more than just executable programs. For example, the two
byte magic number 0x1f 0x8b identifies a particular species of
compressed file (GNU gzip
files).
The magic number is a binary bit pattern, but it may happen to
correspond to printable ASCII characters. This allows magic numbers
to be used in text files. For example, the magic number for
PostScript files is 0x25 0x21, which is %!
, and the magic
number for executable script files is #!
.
When the kernel tries to execute a file, it checks the magic number.
If it is #!
, the kernel reads more bytes, up to a newline
character. All these bytes (not including the #!
itself)
are taken to be the pathname of an interpreter for the rest of the
file, optionally including a single argument. If the interpreter
itself exists and is executable, the kernel fires it up and gives it
the name of the original script file as a command line argument.
For example, a Tcl script (say, /tmp/foo.tcl) can be made executable (for our system) by making the first line be:
#!/local/bin/megatcl -fcausing the kernel to execute:
/local/bin/megatcl -f /tmp/foo.tcl
Note that the first line that the interpreter will see is the
#!
line! This is why Tcl (and many many interpreted
languages that were developed under Unix) uses # as the comment
character!
cat
and ls
commands:
cat /tmp/foo.tcl /etc/motd ls -l /etc/passwdand sometimes the command line arguments are used for completely arbitrary things.
Command line arguments are parameters to Unix programs, much the same way that Tcl procs take arguments. In fact, Tcl's command syntax was modelled on the typical Unix shell syntax for passing command line arguments to programs.
You can read command line arguments that are passed to your Tcl program. There are two special global variables:
argv0
argv
exit
Commandexit
command terminates execution of your Tcl
program. It takes an optional integer argument as the exit status.
If no argument is provided, the exit status will be 0.
There is no way for a child to modify its parent's environment.
Your shell will allow you to set environment variables, with the exact syntax differing from shell to shell. Here's an example of setting the environment variable FOO to the value Armageddon, in several different shells:
FOO=Armageddon; export FOO
export FOO=Armageddon
(these shells also accept the same syntax as sh)
setenv FOO Armageddon
FOO=Armageddon
env
contains an entry for each environment variable.
This not only allows access to any named environment variable, but
allows the use of foreach
and array names
env
to iterate over the entire environment.
set env(HOME) => /home/keith set env(NETHACKOPTIONS) => !pickup,rest_on_space,time,fruit:durian set env(PRINTER) ps => ps array names env => ... parray env => ...
-f
filename
-n
#!
line of debugged scripts to avoid
scaring your users...
-c
Tcl command
--
cmdtrace
Commandcmdtrace onTo turn tracing off, give the command:
cmdtrace off
See the Extended Tcl man page for more details (such as how to control the amount of trace output, and how to redirect the output to a file).
time
Commandtime script ?iterations?
proc profile-script {cmd {sort cpu} {levels 1}} { profile on uplevel $cmd profile off pro profrep pro $sort $levels }
Here is an example profile report from a run of a real Tcl program (my
program check-urls
). Note that the proc
null
is called far more often than any other proc, and
may be a good candidate for optimization (or not). Some of the procs
with the highest CPU time may also be good candidates.
--------------------------------------------------------- Procedure Call Stack Calls Real Time CPU Time --------------------------------------------------------- begin_GLOBAL() 2 88982 6904 eachFile 1 88907 6852 urlSource 1 88885 6818 htmlFile 1 88883 6818 checkUrl 26 87245 5219 checkFtp 3 72685 2728 verify-ftp 3 70835 2213 checkWebPage 22 12438 2063 netgets 99 78384 1959 checkUrlProtoWise 22 12320 1959 ftp-response 24 69640 1784 null 467 856 858 log-url 47 777 774 timeout 31 551 480 hostname 3 1751 446 verbose 86 504 428 ping 3 732 257 unwindProtect 32 231 192 checkNews 1 1722 120 verify-nntp 1 1654 86 nntp-response 3 1500 35 getopt 3 55 35 vardefault 14 30 17 errdefault 5 20 17 envdefault 5 18 17
\&.lib
, rather than \&.tcl
. You should use
\&.tcl
as the extension for your main program. Your main
program can easily make use of the routines in your library files via
autoloading (see below).
make
make
application is
designed to automate the construction of executables from source, and
their installation.
auto_path
variable.
auto_mkindex directory glob patternTo index your library files, use the standard Tcl procedure
auto_mkindex
. It indexes all the files that match
glob pattern and creates an index file in
directory. Example:
auto_mkindex . *.lib
auto_path
is a list of directories
that contain Tcl library files and indices. After creating your
index, you need to add the directory that contains it to the
auto_path
list; here are two examples:
lappend auto_path . set auto_path [linsert $auto_path 0 ~/lib/tcl]Tcl searches for autoload procs in each directory in the
auto_path
in order, using the first one it finds.