Date: Tue, 12 Feb 2013 17:06:54 +0100 From: Fabian Keil <freebsd-listen@fabiankeil.de> To: Devin Teske <Devin.Teske@fisglobal.com> Cc: Tim Daneliuk <tundra@tundraware.com>, FreeBSD Mailing List <freebsd-questions@freebsd.org> Subject: Re: Was I Sourced? Message-ID: <20130212170654.6a649d2c@fabiankeil.de> In-Reply-To: <13CA24D6AB415D428143D44749F57D7201EA755A@ltcfiswmsgmb21> References: <511A5771.9020208@tundraware.com> <13CA24D6AB415D428143D44749F57D7201EA755A@ltcfiswmsgmb21>
next in thread | previous in thread | raw e-mail | index | archive | help
--Sig_/KZrwQe_Fu5D+npVn4AdUVaS Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable "Teske, Devin" <Devin.Teske@fisglobal.com> 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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130212170654.6a649d2c>