Another sed question.

Maarten Litmaath maart at cs.vu.nl
Sat Dec 2 10:15:12 AEST 1989


In article <37091 at ames.arc.nasa.gov> gahooten at orion.arc.nasa.gov (Gregory A. Hooten) writes:
\I ran across a shell script that had this as the first line of code.
\
\-------------------------------------------------------------
\#!/bin/sed d1

Probably:

	#!/bin/sed 1d
	<the rest of the script>

When you try to execute this script, the kernel opens it to find out what kind
of executable it is.  The header of a *binary* includes the size of the text,
data and bss segments etc..  This file, however, isn't a binary: it's an
EXECUTABLE shell script.  The kernel discovers the `#!' MAGIC NUMBER and takes
the following word as the real executable to start.  There may be 1 option
specified.  Suppose the file is called `foo', then

	$ foo

is handled as if you had typed

	$ /bin/sed 1d foo

(See execl(3).)
As you noticed this command will delete the first line of `foo' and print the
rest of the script.  So the script is equivalent to:

	#!/bin/sh
	cat << \EOF
	<the rest of the script>
	EOF

The difference: the first form is faster, because there's no invocation of
/bin/sh.  (In the second form there's no need to use sed instead of cat.)
Here's another nice example (put it in your bin directory):
----------8<----------8<----------8<----------8<----------8<----------
#!/bin/sed 1d
          C operator precedence/associativity chart

Arity       Operator                                    Assoc
--------------------------------------------------------------
mixed     ()  []  ->  .                                 l -> r
unary     !   ~   ++  --  -  (type)  *  &  sizeof       r -> l
binary    *   /   %                                     l -> r
binary    +   -                                         l -> r
binary    <<  >>                                        l -> r
binary    <   <=  >   >=                                l -> r
binary    ==  !=                                        l -> r
binary    &                                             l -> r
binary    ^                                             l -> r
binary    |                                             l -> r
binary    &&                                            l -> r
binary    ||                                            l -> r
ternary   ?:                                            r -> l
binary    = += -= *= /= %= >>= <<= &= ^= |=             r -> l
binary    ,                                             l -> r
--------------------------------------------------------------
-- 
`Take John Berryhill: the guy is everywhere!  All because one day he typed "rn"
instead of [rm]'  (Richard Sexton)  | maart at cs.vu.nl, uunet!mcsun!botter!maart



More information about the Comp.unix.questions mailing list