Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Feb 2013 15:14:37 +0000
From:      "Teske, Devin" <Devin.Teske@fisglobal.com>
To:        "tundra@tundraware.com" <tundra@tundraware.com>, FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   RE: Was I Sourced?
Message-ID:  <13CA24D6AB415D428143D44749F57D7201EA755A@ltcfiswmsgmb21>
In-Reply-To: <511A5771.9020208@tundraware.com>
References:  <511A5771.9020208@tundraware.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 12 Feb 2013, Tim Daneliuk wrote:

> Is there a way for script to determine whether is was sourced
> or forked off as a subprocess when it was invoked?
>=20

Not that I'm aware of.


> I have a script that needs to be sourced to work properly and
> I want to warn the luser if they exec or subshell it instead.
>=20

In order for a user to exec a script it has to have the invocation line:

#!/bin/sh

So naturally, if your script is missing this as the first line, the file wi=
ll not be interpretable (and thus exec will fail). And if the file is not e=
xecutable (due to lack of invocation line) then you also shouldn't have any=
 worry about execution within a sub-shell (though it will still be possible=
 to "source" within a sub-shell using "." -- but that shouldn't be any diff=
erent than sourcing in the main shell).

One thin you could do is to use an invocation line that will warn the user =
that it shouldn't be executed directly...


#!/not_directly_executable # Use the source luke

So when someone executes your script they get...

-bash: ./script: /not_directly_executable: bad interpreter: No such file or=
 directory
--=20
Devin

_____________
The information contained in this message is proprietary and/or confidentia=
l. If you are not the intended recipient, please: (i) delete the message an=
d all copies; (ii) do not disclose, distribute or use the message in any ma=
nner; and (iii) notify the sender immediately. In addition, please be aware=
 that any message addressed to our domain is subject to archiving and revie=
w by persons other than the intended recipient. Thank you.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?13CA24D6AB415D428143D44749F57D7201EA755A>