From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20379 invoked by alias); 12 Sep 2014 20:10:02 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 19076 Received: (qmail 395 invoked from network); 12 Sep 2014 20:09:48 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=X/watNOl04/UWpxq0tG9njeunX/yWZSR6BHUeu57Ayk=; b=k88o986gMBfnjhehiWYTXFgelCr8hirUOJ8y+t/LyRZ0RW86eb4rVnk6H2kpDypPHG POYiJr4L1e/UVwlE38D5p6RhI6gF0c7xrX3RPkpiWP2rKHWVD7l+az4sln/ub4ADPE27 gaxkKaD3N9n6WHjrx6Z28B0tCcs5ukp/ihQ1dsh6MDuk5/4qa07lUSvt1Gva3unirLNR eDCBUlucwiovvrRanpxmtEgC0bguHdWMOK00H6IaQegLyn+18dzWjWYLcbDdW+15plEv LkzQQdsoFvK/wo/BCNxgodU3Ez6zYJdaUOVaOvIq+4MMSps8mIplpN1wUim6sRYOJAJX 9BRw== X-Gm-Message-State: ALoCoQn2q1m24DA8eF0lGuT7vjuE8WFIyPB2mydsLoY9KJbfp9p8/WPy0Rf3fPk9Psiks64GzYfP MIME-Version: 1.0 X-Received: by 10.52.99.106 with SMTP id ep10mr4047105vdb.73.1410552585900; Fri, 12 Sep 2014 13:09:45 -0700 (PDT) In-Reply-To: <541333FF.3090601@thequod.de> References: <541333FF.3090601@thequod.de> Date: Fri, 12 Sep 2014 13:09:45 -0700 Message-ID: Subject: Re: Detect if a script is being sourced vs executed From: Kurtis Rader To: Daniel Hahler Cc: Zsh Users Content-Type: multipart/alternative; boundary=20cf307f30f8d1eec50502e3dd39 --20cf307f30f8d1eec50502e3dd39 Content-Type: text/plain; charset=UTF-8 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 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 --20cf307f30f8d1eec50502e3dd39--