zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Hahler <dhahler@gmail.com>
To: zsh-users@zsh.org
Subject: Detect if a script is being sourced vs executed
Date: Fri, 12 Sep 2014 19:57:19 +0200	[thread overview]
Message-ID: <541333FF.3090601@thequod.de> (raw)

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.


             reply	other threads:[~2014-09-12 17:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-12 17:57 Daniel Hahler [this message]
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

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=541333FF.3090601@thequod.de \
    --to=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).