bash: is script being sourced

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

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

Re: bash: is script being sourced

On Wednesday 13 September 2006 20:39, Vilmos Soti wrote
in comp.os.linux.misc:
> "Angel Tsankov" writes:
>
>> How can a bash script determine if it is being sourced?
>
> What do you want to do?
>
Find an answer for her homework?

--
Robert HULL

Archival or publication of this article on any part of thisishull.net
is without consent and is in direct breach of the Data Protection Act

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"?

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.

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

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.

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

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.

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.

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

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.

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

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.

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.