Staden scripting

Tim Cutts timc at chiark.greenend.org.uk
Tue Jul 3 03:32:54 EST 2001


>I don't quite understand how the dual sh tsch script stuff works, but as
>long as it does then I'm prepared to accept one line of "magic" in the
>script.

>#!/usr/bin/sh
>
># \
>exec gap4sh -f "$0" ${@+"$@"} || exit 1

You don't *have* to do this; it's a way of making the script use the
version of gap4sh that is on your path rather than some hardwired
version.  You could also use:

#!/bin/env gap4sh

to do the same thing, but that's slightly less flexible, and env is not
available on all systems, whereas a bourne compatible shell always is.

The first line makes it initially run as a /usr/bin/sh script (note of
pedantry - that's a very odd place for the bourne shell to be - /bin/sh
is more usual) The second two lines are rather clever.  When executed as
a shell script, only the first line is a comment.  The shell then
executes gap4sh with the script as the first parameter after the -f, and
all the remaining command line parameters tacked on the end.

When gap4sh sees these two lines, it notices the \ at the end of the #
line, and extends the comment onto the next line (the exec command).
Thus, the rest of the script gets executed as tcl.

You have to do this sort of thing on broken systems where #! doesn't
work, or where there's no such facility.  For example, perl scripts on
MS-DOS or Windows 9x/NT/2000 have to do a similar trick; they execute
initially as an MS-DOS batch file, and then run themselves through perl.

Tim.




More information about the Staden mailing list