From owner-freebsd-questions@FreeBSD.ORG Tue Feb 12 17:06:29 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 40C5593D for ; Tue, 12 Feb 2013 17:06:29 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (mx-out.r-bonomi.com [204.87.227.120]) by mx1.freebsd.org (Postfix) with ESMTP id 79089242 for ; Tue, 12 Feb 2013 17:06:27 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.4/rdb1) id r1CHAlNh028236; Tue, 12 Feb 2013 11:10:47 -0600 (CST) Date: Tue, 12 Feb 2013 11:10:47 -0600 (CST) From: Robert Bonomi Message-Id: <201302121710.r1CHAlNh028236@mail.r-bonomi.com> To: tundra@tundraware.com Subject: Re: Was I Sourced? In-Reply-To: <511A5771.9020208@tundraware.com> Cc: freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Feb 2013 17:06:29 -0000 > Date: Tue, 12 Feb 2013 08:53:37 -0600 > From: Tim Daneliuk > To: FreeBSD Mailing List > 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.