[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Script doesn't work when it is run using '#!/bin/sh'

Hash: SHA1

On Sat, Apr 16, 2016 at 03:47:24PM +0100, Aero Maxx wrote:
> On 16/04/2016 14:08, tomas@tuxteam.de wrote:
> >Hash: SHA1
> >
> >On Sat, Apr 16, 2016 at 01:22:09PM +0100, Aero Maxx wrote:
> >>I have a number of bash scripts that work perfectly fine on fedora
> >>23, but do not work on debian 8.  I've tried to sort it out myself,
> >>but am a little stuck now hopefully someone is able to give me a
> >>nudge in the right direction.
> >>The output I get from the above script is
> >>>wordpress_beta.sh: 8: bashtest.sh: [[: not found
> >>>wordpress_beta.sh: 8: bashtest.sh: web: not found
> >>>wordpress_beta.sh: 8: bashtest.sh: web: not found
> >>>
> >>>Setting current directory to 'web' web public folder.
> >The [[ is correct bash syntax. To try to pinpoint the problem, try
> >running the script with "bash <script>". If you don't want to run
> >the script but just to check syntax, you might want to try
> >"bash -n <script>".
> >
> >If that works out OK, perhaps your script is still being invoked
> >under dash?
> I tried that it works thank you, I mistaken assumed that because I
> changed the shebang line to be bash instead of sh,
> and then ran it using "sh <script>" it would work, but obviously I
> got the same errors as before, and I wasn't aware that
> there was one called bash also, but now I think of it that's quite obvious.

Ah, now I see. If you call it explicitly as above, you decide
which shell is used (which is sh, which is dash by default in
Debian [1]).

In that case, the shebang line is interpreted as a comment,
like every line preceded by a # in all shells.

The shebang line unfolds its magic only when you invoke the
script directly, that is "<script>" instead of "sh <script>":
then the kernel looks at the file to find out what "interpreter"
to call. If it's an elf, then it goes to ld.so, if it has a
shebang line then it's the program (and possibly arguments)
mentioned there. There are more possibilities, depending on
the binfmt modules [2]

[1] It used to be bash, but dash is much smaller, lighter and
   faster, so it was decided to use it for all the infrastructure
   (you get faster boots an can build smaller systems for
   limited resources). For day-to-day desktop use, bash or
   zsh are more comfy.

[2] <https://en.wikipedia.org/wiki/Binfmt_misc>

- -- t
Version: GnuPG v1.4.12 (GNU/Linux)


Reply to: