bash: is script being sourced - Help

This is a discussion on bash: is script being sourced - Help ; How can a bash script determine if it is being sourced?...

+ Reply to Thread
Results 1 to 15 of 15

Thread: bash: is script being sourced

  1. bash: is script being sourced

    How can a bash script determine if it is being sourced?

  2. Re: bash: is script being sourced

    "Angel Tsankov" writes:

    > How can a bash script determine if it is being sourced?


    What do you want to do?

    Vilmos

  3. Re: bash: is script being sourced

    On 2006-09-13, Angel Tsankov wrote:
    > How can a bash script determine if it is being sourced?


    If $0 contains bash, or the name of the shell, it is being sourced;
    if it contains the name of the script it is being executed.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  4. Re: bash: is script being sourced

    c>> How can a bash script determine if it is being sourced?
    >
    > If $0 contains bash, or the name of the shell, it is being
    > sourced;
    > if it contains the name of the script it is being executed.


    What about the case when bash is invoked by su and $0 contains
    "-su"?


  5. Re: bash: is script being sourced

    "Chris F.A. Johnson" writes:

    > On 2006-09-13, Angel Tsankov wrote:
    >> How can a bash script determine if it is being sourced?

    >
    > If $0 contains bash, or the name of the shell, it is being sourced;
    > if it contains the name of the script it is being executed.


    Not really:

    #v+
    $ cat test.sh
    echo "\$0: $0"
    if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
    $ bash test.sh
    $0: test.sh
    source test.sh
    $0: test.sh
    #v-

    In general there's probably no way to determine this.

    --
    Best regards, _ _
    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    ooo +------ooO--(_)--Ooo--

  6. Re: bash: is script being sourced

    On 2006-09-14, Michal Nazarewicz wrote:
    > "Chris F.A. Johnson" writes:
    >
    >> On 2006-09-13, Angel Tsankov wrote:
    >>> How can a bash script determine if it is being sourced?

    >>
    >> If $0 contains bash, or the name of the shell, it is being sourced;
    >> if it contains the name of the script it is being executed.

    >
    > Not really:
    >
    > #v+
    > $ cat test.sh
    > echo "\$0: $0"
    > if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
    > $ bash test.sh
    > $0: test.sh
    > source test.sh
    > $0: test.sh
    > #v-
    >
    > In general there's probably no way to determine this.


    True, there's no absolute method.

    A more accurate statement is: if $0 contains the name of the
    calling program, then the script is being sourced. If it contains
    the name of the script itself, then it is either being executed, or
    it has sourced itself.

    If the script never sources itself, then it is possible to tell
    (assuming that you have the name of the script hard coded and that
    it hasn't been changed).

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  7. Re: bash: is script being sourced

    >>>> How can a bash script determine if it is being sourced?
    >>>
    >>> If $0 contains bash, or the name of the shell, it is being
    >>> sourced;
    >>> if it contains the name of the script it is being
    >>> executed.

    >>
    >> Not really:
    >>
    >> #v+
    >> $ cat test.sh
    >> echo "\$0: $0"
    >> if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
    >> $ bash test.sh
    >> $0: test.sh
    >> source test.sh
    >> $0: test.sh
    >> #v-
    >>
    >> In general there's probably no way to determine this.

    >
    > True, there's no absolute method.


    In fact, I need to determine the full path to the script that is
    being executed, either sourced or not.


  8. Re: bash: is script being sourced

    On 2006-09-14, Angel Tsankov wrote:
    >>>>> How can a bash script determine if it is being sourced?
    >>>>
    >>>> If $0 contains bash, or the name of the shell, it is being
    >>>> sourced;
    >>>> if it contains the name of the script it is being
    >>>> executed.
    >>>
    >>> Not really:
    >>>
    >>> #v+
    >>> $ cat test.sh
    >>> echo "\$0: $0"
    >>> if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
    >>> $ bash test.sh
    >>> $0: test.sh
    >>> source test.sh
    >>> $0: test.sh
    >>> #v-
    >>>
    >>> In general there's probably no way to determine this.

    >>
    >> True, there's no absolute method.

    >
    > In fact, I need to determine the full path to the script that is
    > being executed, either sourced or not.


    Why?

    If you want the path to a script use 'type -p SCRIPTNAME.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  9. Re: bash: is script being sourced

    "Angel Tsankov" writes:

    >>>>> How can a bash script determine if it is being sourced?
    >>>>
    >>>> If $0 contains bash, or the name of the shell, it is being
    >>>> sourced;
    >>>> if it contains the name of the script it is being executed.
    >>>
    >>> Not really:
    >>>
    >>> #v+
    >>> $ cat test.sh
    >>> echo "\$0: $0"
    >>> if [ $# -eq 0 ]; then echo "source test.sh"; . test.sh foo; fi
    >>> $ bash test.sh
    >>> $0: test.sh
    >>> source test.sh
    >>> $0: test.sh
    >>> #v-
    >>>
    >>> In general there's probably no way to determine this.

    >>
    >> True, there's no absolute method.

    >
    > In fact, I need to determine the full path to the script that is being
    > executed, either sourced or not.


    Take a look at the UNIX programming FAQ question 1.14:

    http://www.faqs.org/faqs/unix-faq/programmer/faq/

  10. Re: bash: is script being sourced

    >> In fact, I need to determine the full path to the script that
    >> is
    >> being executed, either sourced or not.

    >
    > Why?
    >
    > If you want the path to a script use 'type -p SCRIPTNAME.


    I have a script that installs ceratin packages and assumes that
    they are located in a directory relative to its location. The
    location of the script, however, is not known in advance, i.e. it
    may be on a cdrom or a fd.


  11. Re: bash: is script being sourced

    On 2006-09-14, Angel Tsankov wrote:
    >>> In fact, I need to determine the full path to the script that
    >>> is
    >>> being executed, either sourced or not.

    >>
    >> Why?
    >>
    >> If you want the path to a script use 'type -p SCRIPTNAME.

    >
    > I have a script that installs ceratin packages and assumes that
    > they are located in a directory relative to its location.


    Fix the script.

    > The location of the script, however, is not known in advance, i.e.
    > it may be on a cdrom or a fd.


    If it's not known, how can you execute it?

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  12. Re: bash: is script being sourced

    >>>> In fact, I need to determine the full path to the script
    >>>> that
    >>>> is
    >>>> being executed, either sourced or not.
    >>>
    >>> Why?
    >>>
    >>> If you want the path to a script use 'type -p SCRIPTNAME.

    >>
    >> I have a script that installs ceratin packages and assumes
    >> that
    >> they are located in a directory relative to its location.

    >
    > Fix the script.
    >
    >> The location of the script, however, is not known in advance,
    >> i.e.
    >> it may be on a cdrom or a fd.

    >
    > If it's not known, how can you execute it?


    I meant that the script would not know its own location. However,
    users do not where the script is located and they are free to
    move it to a different folder.


  13. Re: bash: is script being sourced

    On 2006-09-15, Angel Tsankov wrote:
    >>>>> In fact, I need to determine the full path to the script
    >>>>> that
    >>>>> is
    >>>>> being executed, either sourced or not.
    >>>>
    >>>> Why?
    >>>>
    >>>> If you want the path to a script use 'type -p SCRIPTNAME.
    >>>
    >>> I have a script that installs ceratin packages and assumes
    >>> that
    >>> they are located in a directory relative to its location.

    >>
    >> Fix the script.
    >>
    >>> The location of the script, however, is not known in advance,
    >>> i.e.
    >>> it may be on a cdrom or a fd.

    >>
    >> If it's not known, how can you execute it?

    >
    > I meant that the script would not know its own location.


    The script shouldn't need to know its own location.

    > However, users do not where the script is located and they are free
    > to move it to a different folder.


    If they move it, they know where it is, both origin and
    destination.

    Configuration files belong in a specific location. System-wide
    files belong in /etc; users' own configuration files belong in
    $HOME, or some directory under it.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  14. Re: bash: is script being sourced

    >>>>>> In fact, I need to determine the full path to the script
    >>>>>> that
    >>>>>> is
    >>>>>> being executed, either sourced or not.
    >>>>>
    >>>>> Why?
    >>>>>
    >>>>> If you want the path to a script use 'type -p SCRIPTNAME.
    >>>>
    >>>> I have a script that installs ceratin packages and assumes
    >>>> that
    >>>> they are located in a directory relative to its location.
    >>>
    >>> Fix the script.
    >>>
    >>>> The location of the script, however, is not known in
    >>>> advance,
    >>>> i.e.
    >>>> it may be on a cdrom or a fd.
    >>>
    >>> If it's not known, how can you execute it?

    >>
    >> I meant that the script would not know its own location.

    >
    > The script shouldn't need to know its own location.


    OK, but it needs to know where the packages are.


  15. Re: bash: is script being sourced

    "Angel Tsankov" writes:

    >>>>>>> In fact, I need to determine the full path to the script
    >>>>>>> that
    >>>>>>> is
    >>>>>>> being executed, either sourced or not.
    >>>>>>
    >>>>>> Why?
    >>>>>>
    >>>>>> If you want the path to a script use 'type -p SCRIPTNAME.
    >>>>>
    >>>>> I have a script that installs ceratin packages and assumes
    >>>>> that
    >>>>> they are located in a directory relative to its location.
    >>>>
    >>>> Fix the script.
    >>>>
    >>>>> The location of the script, however, is not known in advance,
    >>>>> i.e.
    >>>>> it may be on a cdrom or a fd.
    >>>>
    >>>> If it's not known, how can you execute it?
    >>>
    >>> I meant that the script would not know its own location.

    >>
    >> The script shouldn't need to know its own location.

    >
    > OK, but it needs to know where the packages are.


    Capture that during install.

+ Reply to Thread