From: Kurtis Rader <krader@skepticism.us>
To: Daniel Hahler <dhahler@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Detect if a script is being sourced vs executed
Date: Fri, 12 Sep 2014 13:09:45 -0700 [thread overview]
Message-ID: <CABx2=D-6GF0wkEbASjyxQGkgP_kBwKYBpSguhXGbUyj9eWq-Ow@mail.gmail.com> (raw)
In-Reply-To: <541333FF.3090601@thequod.de>
[-- Attachment #1: Type: text/plain, Size: 2506 bytes --]
Here's an example of what I use in my scripts (zsh 5.0.2):
if [[ $ZSH_EVAL_CONTEXT == 'toplevel' ]]; then
# We're not being sourced so run the colors command which in turn
sources
# this script and uses its content to produce representative output.
colors
fi
On Fri, Sep 12, 2014 at 10:57 AM, Daniel Hahler <dhahler@gmail.com> wrote:
> I want to detect if a script is being sourced instead of being called.
>
> This is meant for pyenv, which uses shims to delegate execution to the
> selected Python version.
>
> Currently it always uses "exec" to do so, but there is a pull request to
> add support for scripts that are meant to be sourced (like
> virtualenvwrapper.sh), and then would use "source" instead of "exec" in the
> shim.
>
> The PR provides this functionality for Bash, which provides a way to
> detect this via $BASH_SOURCE/$BASH_LINENO:
> https://github.com/yyuu/pyenv/pull/100/files
>
>
> My current approach so far is the following, which requires Zsh 5.0.5 (for
> POSIX_ARGZERO) and does not work for "zsh test_source.sh" yet (a patch has
> been posted to zsh-workers to fix this).
>
> I would like to do this for older Zsh versions, and in a simpler / more
> elegant way.
>
>
> Script to be sourced (test_source.sh):
>
> #!/usr/bin/zsh
>
> echo "\$0: $0"
>
> sourced=0
> if [ -n "$ZSH_VERSION" ]; then
> # Use prompt expansion to get the current file.
> cur_file=${(%):-%x}
>
> # Fix $0:
> if (( ${+options[posixargzero]} )); then # added in zsh 5.0.5
> (2014-06-01)
> if [[ $options[posixargzero] != "on" ]]; then
> setopt posixargzero
> [ "$0" = "$cur_file" ] || sourced=1
> setopt noposixargzero
> else
> [ "$0" = "$cur_file" ] || sourced=1
> fi
> else
> echo "TODO"
> fi
>
> elif [ -n "$BASH_VERSION" ]; then
> [ "$0" = "$BASH_SOURCE" ] || sourced=1
> fi
>
> echo "sourced: $sourced"
>
>
> Test script (test.sh):
>
> #!/usr/bin/zsh
>
> echo "== CALL"
> ./test_source.sh
>
> echo "== SOURCE"
> . ./test_source.sh
>
> echo "== CALL: bash"
> bash ./test_source.sh
>
> echo "== CALL: zsh"
> zsh ./test_source.sh
>
> echo "== EXEC"
> exec ./test_source.sh
>
>
> There must be an easier way!?
>
> It would be really useful, if Zsh would provide a mechanism like Bash to
> simplify this.
>
>
> Regards,
> Daniel.
>
--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
prev parent reply other threads:[~2014-09-12 20:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-12 17:57 Daniel Hahler
2014-09-12 19:05 ` Peter Stephenson
2014-09-12 23:21 ` Bart Schaefer
2014-09-13 8:01 ` Peter Stephenson
2014-09-16 19:14 ` Peter Stephenson
2014-09-12 20:09 ` Kurtis Rader [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CABx2=D-6GF0wkEbASjyxQGkgP_kBwKYBpSguhXGbUyj9eWq-Ow@mail.gmail.com' \
--to=krader@skepticism.us \
--cc=dhahler@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).