Was I Sourced?

Teske, Devin Devin.Teske at fisglobal.com
Tue Feb 12 15:14:45 UTC 2013


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?
> 

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.
> 

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 will not be interpretable (and thus exec will fail). And if the file is not executable (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 different 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
-- 
Devin

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


More information about the freebsd-questions mailing list