45.4 Fun with #!
[You might think that the "magic" characters
Q:
Why begin a shell script
with A: Under some systems - principally those with Berkeley influence - this makes the program directly executable. That is, the kernel can start this program, even though it's not machine code; the kernel will invoke the named program after fiddling arguments a bit. In fact, the script:
#! /bin/mv
will rename itself. Place it in a file called
zap
,
and type You can make self-removing scripts:
#! /bin/rm Or self-printing scripts:
#! /bin/awk NR>1{print} text... This last one works because the kernel is willing to do more than insert the filename in the argument list: it will insert an optional argument. Some systems allow only one such argument (which can then contain blanks), while others allow multiple blank (space or TAB) separated arguments. Note that, in the latter case, there is probably no way to embed blanks (the kernel is unlikely to provide shell-like quoting mechanisms); the safest course is to avoid them entirely. Normally, this is used for things like the -f option to the C shell ("fast", don't read .cshrc s), but it works well enough for awk too.
- in net.unix on Usenet, 29 December 1984 |
|