The main script is the primary script that is executed by the interpreter, e.g. tclsh or wish.
One common technique is to have a script run a self test if it detects that it is the main script. Naive approaches only compare info script with $argv0. A somewhat more robust approach that usually works with pkg_mkIndex looks only at the file tail of those two values. The most robust approach fully normalizes those two values, including the last component, which a simple file normalize does not resolve.A
The most complete approach:
if {[info exists argv0] && [ file dirname [file normalize [info script]/...]] eq [ file dirname [file normalize $argv0/...]]} { #do stuff }
AMG: I don't understand the purpose of the /... in this example.
A fairly robust approach:
if {[info exists argv0] && [file tail [info script]] eq [file tail $argv0]} { #do stuff }
Another variation:
if {![info exists argv0] || [ file tail [info script]] ne [file tail $argv0]} return # test/standalone code follows
The more simple and naive approach to detection method only compares info script with $argv0:
if {[info exists argv0] && $argv0 eq [info script]} { #do stuff }
or, as formulated in a comp.lang.tcl posting by DKF:
if {[string equal $::argv0 [info script]] || [ array exists ::embed_args]} { main }