Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Feb 2013 11:10:47 -0600 (CST)
From:      Robert Bonomi <bonomi@mail.r-bonomi.com>
To:        tundra@tundraware.com
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Was I Sourced?
Message-ID:  <201302121710.r1CHAlNh028236@mail.r-bonomi.com>
In-Reply-To: <511A5771.9020208@tundraware.com>

next in thread | previous in thread | raw e-mail | index | archive | help

> Date: Tue, 12 Feb 2013 08:53:37 -0600
> From: Tim Daneliuk <tundra@tundraware.com>
> To: FreeBSD Mailing List <freebsd-questions@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.





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