Shell puzzle 2
November 29, 2007 1:41 pm GeneralFollowing 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.
November 29th, 2007 at 2:27 pm
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
November 29th, 2007 at 3:14 pm
I think that #! only allows you to specify one additional parameter?
November 29th, 2007 at 3:25 pm
Because all text after the initial interpreter name is passed in the first argument.
$ /bin/sh “-e -x”
November 29th, 2007 at 3:43 pm
It runs here (Solaris) fine without error:
~ $ cat /tmp/p.sh
#!/bin/sh -e -x
true
~ $ /tmp/p.sh
~ $
Linux issue?
November 29th, 2007 at 4:05 pm
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’.
November 30th, 2007 at 6:53 pm
Marius: he’s asking a riddle, not wondering why the script fails. Scott got the answer.