Shell puzzle 2

1:41 pm General

Following on from this shell script puzzle here’s another.

Why does the following script give this error?

sh: Illegal option –

(Credit to Kevin Lyda of ILUG for the puzzle and its truly twisted answer).

#! /bin/sh -e -x

true

Note that #! /bin/sh -e, #! /bin/sh -x and #! /bin/sh -ex run without error.

6 Responses

  1. Raphaƫl Quinet Says:

    Your script could work on some Unix variants such as BSDi, older FreeBSDs or Minix.

    Some hints can be found in the following table:
    http://www.in-ulm.de/~mascheck/various/shebang/#results

  2. fraggle Says:

    I think that #! only allows you to specify one additional parameter?

  3. Scott Robinson Says:

    Because all text after the initial interpreter name is passed in the first argument.

    $ /bin/sh “-e -x”

  4. m Says:

    It runs here (Solaris) fine without error:

    ~ $ cat /tmp/p.sh
    #!/bin/sh -e -x

    true
    ~ $ /tmp/p.sh
    ~ $

    Linux issue?

  5. Marius Gedminas Says:

    Try this instead:

    #!/bin/sh
    set -e -x

    It will work on all Unixes, and it will work when you run the script with ‘sh script.sh’.

  6. Rudd-O Says:

    Marius: he’s asking a riddle, not wondering why the script fails. Scott got the answer.

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.