From owner-freebsd-questions@FreeBSD.ORG Tue Feb 12 16:10:46 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 8F43D9FD for ; Tue, 12 Feb 2013 16:10:46 +0000 (UTC) (envelope-from freebsd-listen@fabiankeil.de) Received: from smtprelay04.ispgateway.de (smtprelay04.ispgateway.de [80.67.18.16]) by mx1.freebsd.org (Postfix) with ESMTP id 34C34EBA for ; Tue, 12 Feb 2013 16:10:45 +0000 (UTC) Received: from [78.35.129.207] (helo=fabiankeil.de) by smtprelay04.ispgateway.de with esmtpsa (SSLv3:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1U5IQ1-0007RW-ON; Tue, 12 Feb 2013 17:09:17 +0100 Date: Tue, 12 Feb 2013 17:06:54 +0100 From: Fabian Keil To: Devin Teske Subject: Re: Was I Sourced? Message-ID: <20130212170654.6a649d2c@fabiankeil.de> In-Reply-To: <13CA24D6AB415D428143D44749F57D7201EA755A@ltcfiswmsgmb21> References: <511A5771.9020208@tundraware.com> <13CA24D6AB415D428143D44749F57D7201EA755A@ltcfiswmsgmb21> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/KZrwQe_Fu5D+npVn4AdUVaS"; protocol="application/pgp-signature" X-Df-Sender: Nzc1MDY3 Cc: Tim Daneliuk , FreeBSD Mailing List 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 16:10:46 -0000 --Sig_/KZrwQe_Fu5D+npVn4AdUVaS Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable "Teske, Devin" wrote: > On Tue, 12 Feb 2013, Tim Daneliuk wrote: >=20 > > Is there a way for script to determine whether is was sourced > > or forked off as a subprocess when it was invoked? > >=20 >=20 > Not that I'm aware of. sysutils/zogftw, which has to find and parse itself to generate the verbose help, does the following: zogftw_location=3D"$0" if [ "zogftw" !=3D $(basename "${zogftw_location}") ]; then # Looks like zogftw has been sourced. # Try to get the zogftw location through the PATH. zogftw_location=3D"$(which zogftw)" fi The check is expected to fail if the user renamed the shell to zogftw, or the script to something else, but that's unlikely to happen by accident and the functionality is not essential anyway. A more reliable method might be investigating $$ and its parents with ps and friends, but it would also require a lot more code. I don't remember ever having never seen a ps-based check in the real world and my impression is that looking at $0 is "best practice" if the check doesn't have to be perfect. Fabian --Sig_/KZrwQe_Fu5D+npVn4AdUVaS Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (FreeBSD) iEYEARECAAYFAlEaaKYACgkQBYqIVf93VJ0d8QCgifpZzsb069VgZ6dSLdiYO6d4 XjYAn05BhTQMJ/3x3+rLePwNpM4/Z4J3 =fHzq -----END PGP SIGNATURE----- --Sig_/KZrwQe_Fu5D+npVn4AdUVaS--