zsh-users
 help / color / mirror / code / Atom feed
* Detect if a script is being sourced vs executed
@ 2014-09-12 17:57 Daniel Hahler
  2014-09-12 19:05 ` Peter Stephenson
  2014-09-12 20:09 ` Kurtis Rader
  0 siblings, 2 replies; 6+ messages in thread
From: Daniel Hahler @ 2014-09-12 17:57 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-09-16 19:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-12 17:57 Detect if a script is being sourced vs executed 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

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).