shell script source getting actual path problem

shell script source getting actual path problem

am 26.11.2007 20:47:45 von unknown

I am facing this problem:

I have two scripts, for example:

/home/usr1/dir1/script1
and
/home/usr1/dir2/script2

script1 contains the line:

source /home/usr1/dir2/script2

I am editing script2 and need to know its actual path without editing
script1 -for example sending parameters-

Thanks,
Amr Bedair

Re: shell script source getting actual path problem

am 26.11.2007 20:53:16 von Cyrus Kriticos

mero wrote:
>
> I have two scripts, for example:
>
> /home/usr1/dir1/script1
> and
> /home/usr1/dir2/script2
>
> script1 contains the line:
>
> source /home/usr1/dir2/script2
>
> I am editing script2 and need to know its actual path without editing
> script1 -for example sending parameters-

with bash version >= 3.0:

MYLOCATION="${PWD}/${BASH_ARGV[0]}"
export MYLOCATION="${MYLOCATION%/*}"

--
Best regards | (\_/)
Cyrus | (O.o) This is Bunny. Copy Bunny into your signature
| (> <) to help him on his way to world domination.

Re: shell script source getting actual path problem

am 26.11.2007 21:06:27 von Cyrus Kriticos

Cyrus Kriticos wrote:
> mero wrote:
>>
>> I have two scripts, for example:
>>
>> /home/usr1/dir1/script1
>> and
>> /home/usr1/dir2/script2
>>
>> script1 contains the line:
>>
>> source /home/usr1/dir2/script2
>>
>> I am editing script2 and need to know its actual path without editing
>> script1 -for example sending parameters-
>
> with bash version >= 3.0:
>
> MYLOCATION="${PWD}/${BASH_ARGV[0]}"
> export MYLOCATION="${MYLOCATION%/*}"

correction:
with bash version >= 3.0:

echo ${BASH_ARGV[0]}

--
Best regards | (\_/)
Cyrus | (O.o) This is Bunny. Copy Bunny into your signature
| (> <) to help him on his way to world domination.

Re: shell script source getting actual path problem

am 26.11.2007 22:52:21 von unknown

On Nov 26, 10:06 pm, Cyrus Kriticos
wrote:
> Cyrus Kriticos wrote:
> > mero wrote:
>
> >> I have two scripts, for example:
>
> >> /home/usr1/dir1/script1
> >> and
> >> /home/usr1/dir2/script2
>
> >> script1 contains the line:
>
> >> source /home/usr1/dir2/script2
>
> >> I am editing script2 and need to know its actual path without editing
> >> script1 -for example sending parameters-
>
> > with bash version >= 3.0:
>
> > MYLOCATION="${PWD}/${BASH_ARGV[0]}"
> > export MYLOCATION="${MYLOCATION%/*}"
>
> correction:
> with bash version >= 3.0:
>
> echo ${BASH_ARGV[0]}
>
> --
> Best regards | (\_/)
> Cyrus | (O.o) This is Bunny. Copy Bunny into your signature
> | (> <) to help him on his way to world domination.

is there any way to do this on :
GNU bash, version 2.05b

Thanks,
Amr Bedair

Re: shell script source getting actual path problem

am 26.11.2007 22:55:16 von unknown

On Nov 26, 11:52 pm, mero wrote:
> On Nov 26, 10:06 pm, Cyrus Kriticos
> wrote:
>
>
>
> > Cyrus Kriticos wrote:
> > > mero wrote:
>
> > >> I have two scripts, for example:
>
> > >> /home/usr1/dir1/script1
> > >> and
> > >> /home/usr1/dir2/script2
>
> > >> script1 contains the line:
>
> > >> source /home/usr1/dir2/script2
>
> > >> I am editing script2 and need to know its actual path without editing
> > >> script1 -for example sending parameters-
>
> > > with bash version >= 3.0:
>
> > > MYLOCATION="${PWD}/${BASH_ARGV[0]}"
> > > export MYLOCATION="${MYLOCATION%/*}"
>
> > correction:
> > with bash version >= 3.0:
>
> > echo ${BASH_ARGV[0]}
>
> > --
> > Best regards | (\_/)
> > Cyrus | (O.o) This is Bunny. Copy Bunny into your signature
> > | (> <) to help him on his way to world domination.
>
> is there any way to do this on :
> GNU bash, version 2.05b
>
> Thanks,
> Amr Bedair

Also i need to know please what to do for KSH

Thanks

Re: shell script source getting actual path problem

am 27.11.2007 00:42:13 von cfajohnson

On 2007-11-26, mero wrote:
>
> I am facing this problem:
>
> I have two scripts, for example:
>
> /home/usr1/dir1/script1
> and
> /home/usr1/dir2/script2
>
> script1 contains the line:
>
> source /home/usr1/dir2/script2
>
> I am editing script2 and need to know its actual path without editing
> script1 -for example sending parameters-

grep 'source.* /script2' /home/usr1/dir1/script1

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

Re: shell script source getting actual path problem

am 27.11.2007 13:22:10 von Maxwell Lol

mero writes:

>
> I am editing script2 and need to know its actual path without editing
> script1 -for example sending parameters-

I like to use environment variables for issues like this.

Re: shell script source getting actual path problem

am 27.11.2007 17:04:06 von unknown

On Nov 27, 1:42 am, "Chris F.A. Johnson" wrote:
> On 2007-11-26, mero wrote:
>
> > I am facing this problem:
>
> > I have two scripts, for example:
>
> > /home/usr1/dir1/script1
> > and
> > /home/usr1/dir2/script2
>
> > script1 contains the line:
>
> > source /home/usr1/dir2/script2
>
> > I am editing script2 and need to know its actual path without editing
> > script1 -for example sending parameters-
>
> grep 'source.* /script2' /home/usr1/dir1/script1
>
> --
> Chris F.A. Johnson, author
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence

the problem is that i actually do not know the caller, also it may be
more than one caller

Thanks.

Re: shell script source getting actual path problem

am 27.11.2007 17:04:37 von unknown

On Nov 27, 2:22 pm, Maxwell Lol wrote:
> mero writes:
>
> > I am editing script2 and need to know its actual path without editing
> > script1 -for example sending parameters-
>
> I like to use environment variables for issues like this.

Please, can you explain how to do that?

Thanks

Re: shell script source getting actual path problem

am 27.11.2007 17:04:50 von unknown

On Nov 27, 2:22 pm, Maxwell Lol wrote:
> mero writes:
>
> > I am editing script2 and need to know its actual path without editing
> > script1 -for example sending parameters-
>
> I like to use environment variables for issues like this.

Please, can you explain how to do that?

Thanks

Re: shell script source getting actual path problem

am 27.11.2007 17:07:44 von unknown

On Nov 27, 2:22 pm, Maxwell Lol wrote:
> mero writes:
>
> > I am editing script2 and need to know its actual path without editing
> > script1 -for example sending parameters-
>
> I like to use environment variables for issues like this.

Please, can you explain how to do that?

Thanks

Re: shell script source getting actual path problem

am 27.11.2007 18:23:12 von unknown

On Nov 27, 2:22 pm, Maxwell Lol wrote:
> mero writes:
>
> > I am editing script2 and need to know its actual path without editing
> > script1 -for example sending parameters-
>
> I like to use environment variables for issues like this.

Please, can you explain how to do that?

Thanks

Re: shell script source getting actual path problem

am 27.11.2007 18:35:26 von unknown

On Nov 27, 2:22 pm, Maxwell Lol wrote:
> mero writes:
>
> > I am editing script2 and need to know its actual path without editing
> > script1 -for example sending parameters-
>
> I like to use environment variables for issues like this.

Please, can you explain how to do that?

Thanks

Re: shell script source getting actual path problem

am 28.11.2007 02:03:34 von brian_hiles

mero wrote:
> mero wrote:
> > [script1.bash]
> > source /home/usr1/dir2/script2
> > --
> > What is the actual path of script2?
> Also i need to know please what to do for KSH

See my [ksh and presumably bash] function "resolvepath":

http://groups.google.com/group/comp.unix.shell/browse_thread /thread/77fc9b7c7ca02424/83b2b4fef10d3717?lnk=st&q=%22functi on+resolvepath%22+group%3Acomp.unix.shell&rnum=2&hl=en#83b2b 4fef10d3717

However, a perpetual implementation feature of sourced scripts/
functions (at least in ksh) is that the file argument is liable to a
path lookup. With a little care in controlling PATH, you can let
the system resolve the system pathname, without bothering to
hardwire in fully qualified pathnames into scripts.

PATH=/path/to/scripts . script2 # sources "script2" wherever it exist
in PATH
PATH=/path/to/scripts command . script2 # same, but keeps old PATH

Autoloading (RTFM) is especially appropriate for this purpose.

I understand this doesn't help you now, with a script that you
cannot edit.

=Brian

Re: shell script source getting actual path problem

am 28.11.2007 03:58:19 von Maxwell Lol

mero writes:

> On Nov 27, 2:22 pm, Maxwell Lol wrote:
> > mero writes:
> >
> > > I am editing script2 and need to know its actual path without editing
> > > script1 -for example sending parameters-
> >
> > I like to use environment variables for issues like this.
>
> Please, can you explain how to do that?

Well, if the location of scripts can be in different positions,
the best thing to do is to use a variable that specifies where they are.

Let's say you want to execute /usr/local/bin/MyScript
but it might be in /home/local/bin/MyScript, then just use


${MYSCRIPT='/usr/local/bin'}/MyScript


And then you can, if you want, source a file that has


MYSCRIPT=/home/local/bin

Let's say this file is called ~/.myscript

You can make it unique per user using some code like
----------------------------
if [ -f ~/.myscript ]
then
. ~/.myscript
fi
${MYSCRIPT='/usr/local/bin'}/MyScript
----------------------------


This can be a global script.
The user can either create a startup file called ~/.myscript
or can set an environment variable overriding the default value of MYSCRIPT
Or they can do nothing.