Was I Sourced?

Tim Daneliuk tundra at tundraware.com
Tue Feb 12 18:41:20 UTC 2013


On 02/12/2013 11:10 AM, Robert Bonomi wrote:
>> Date: Tue, 12 Feb 2013 08:53:37 -0600
>> From: Tim Daneliuk <tundra at tundraware.com>
>> To: FreeBSD Mailing List <freebsd-questions at freebsd.org>
>> Subject: Was I Sourced?
>>
>> Is there a way for script to determine whether is was sourced
>> or forked off as a subprocess when it was invoked?
>>
>> 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.
>
> a 'sourced' script does -not- honor a shebag line.
> you can exploit that.
>
> The executable script /usr/local/bin/source_only;
>     #!/bin/sh
>     echo  Error: this script must be sourced
>
> Your script:
>     #!/usr/local/bin/source_only
>
>     {cmd}
>     {cmd}
>     {cmd}
>     {cmd}
>     {cmd}
>     {cmd}
>     ...
>     ...
>
> Trying to do it totally self-contained is not easy.
>
>


Actually, it's not that hard.  Setting the shebang line to this does
the trick:
   
   #!/bin/echo "This Script Must Be Sourced!!!!"
   

Thanks to all who replied on this one ...

   

-- 
-----------------------------------------------------------------------
Tim Daneliuk


More information about the freebsd-questions mailing list