Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script sourced by bash -- can it determine it's own location?

44 views
Skip to first unread message

goo...@scottg.net

unread,
Sep 16, 2007, 7:24:11 PM9/16/07
to
I'm trying to figure out how a script that is sourced at the command
line can determine it's own location, such that for the file ~/test/
script.sh:

# script.sh
export MYLOCATION=???

when I do:

bash# cd $HOME
bash# source test/script.sh

or

bash# cd /etc
bash# source /home/scott/test/script.sh

MYLOCATION is set to be the directory where script.sh lives which is,
in all cases, the same each time.

thanks,

/s.

Icarus Sparry

unread,
Sep 16, 2007, 11:05:07 PM9/16/07
to

In general you can't.

You can use 'history 1' to see how the command was entered, and then
write something to parse that. This will give you something that works in
many cases. However it is by no means foolproof.

Message has been deleted
Message has been deleted

Cyrus Kriticos

unread,
Sep 17, 2007, 12:28:32 AM9/17/07
to
goo...@scottg.net wrote:
> I'm trying to figure out how a script that is sourced at the command
> line can determine it's own location, such that for the file ~/test/
> script.sh:
>
> # script.sh
> export MYLOCATION=???

with bash version >= 3.0:

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

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide

goo...@scottg.net

unread,
Sep 17, 2007, 9:07:47 AM9/17/07
to
Thanks, Cyrus, your code below worked and I have control over what
shell is available on the system. I couldn't find a clean way to
normalize the path name when a relative path to the source file was
used, but that's easily worked around by changing into the target's
directory and using PWD to set the var before jumping back to the
previous directory.

/s.

On Sep 17, 12:28 am, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:

bsh

unread,
Sep 17, 2007, 8:10:40 PM9/17/07
to
On Sep 17, 6:07 am, goo...@scottg.net wrote:
> I couldn't find a clean way to
> normalize the path name when a relative path to the source file was
> used, ...

The resolution of the pathname in $0, and its normalization, via
symlinks
and otherwise, is addressed in my script "resolvepath" below:

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

It's a trickier problem than is generally realized, as there are many
problematic cases. The only problem is that it's a ksh(1) function.
Bash3
_should_ work without editing, although I haven't tested this.

I think you want: resolvepath -lp "$0"

=Brian

Stephane CHAZELAS

unread,
Sep 18, 2007, 3:05:10 AM9/18/07
to
2007-09-17, 17:10(-07), bsh:
[...]

> I think you want: resolvepath -lp "$0"
[...]

I've not had a look at your script, but I suspect it will not
work. $0 contains the path of the script or "bash", not the path
of the currently sourced file. bash (recent versions) has
"${BASH_SOURCE[0]}" for that.

--
Stéphane

bsh

unread,
Sep 19, 2007, 8:10:18 PM9/19/07
to
On Sep 18, 12:05 am, Stephane CHAZELAS <this.addr...@is.invalid>
wrote:

> 2007-09-17, 17:10(-07), bsh:
> > ...
> I've not had a look at your script, but I suspect it will not
> work. $0 contains the path of the script or "bash", not the path
> of the currently sourced file. bash (recent versions) has
> "${BASH_SOURCE[0]}" for that.

You're right, Stéphane. The OQ is partially elided in my
reader, and I did not correctly understand its intention. To
answer this -- and resolvepath may still be of some assistance
-- is that sh and ksh88 do not assign $0 other than the path of
the invoking script, but that _latter versions_ of ksh93 do
indeed set the shell parameter ".sh.fun" to the name of the
current function.

I've performed a workaround in the past where I've explicitly
preloaded the environment of the function with its name. IIRC,
it's something like:

alias foo='SOURCE_NAME=foo foo' # do for each function ...
foo { print $SOURCE_NAME; }
foo

An enumerated list of function names may even be generated with the
"typeset +f" command, and used to feed the above statement in a loop.
I vaguely remember something about there needing to be a pseudo-
autoload to preset the function namespace if the list was to be
generated
before the functions were themselves defined.

# not tested
for f in $( typeset +f )
do eval "alias $f='SOURCE_NAME=$f $f'"
done

=Brian

Tiago Peczenyj

unread,
Sep 19, 2007, 8:41:10 PM9/19/07
to
$ pwd
/home/peczenyj

$ cat test/script.sh
set | grep BASH_ARGV | cut -d\" -f2 | head -1 | xargs -i echo ${PWD}/
{} > /tmp/xxx
read MYVAR < /tmp/xxx

$ source test/script

$ echo $MYVAR
/home/visitante/test/script.sh

:)

bsh

unread,
Sep 20, 2007, 5:50:01 PM9/20/07
to
Tiago Peczenyj <tiago.pecze...@gmail.com> wrote:
> $ pwd
> /home/peczenyj
> $ cat test/script.sh
> set | grep BASH_ARGV | cut -d\" -f2 | head -1 | xargs -i echo ${PWD}/
> {} > /tmp/xxx
> read MYVAR < /tmp/xxx
> $ source test/script
> $ echo $MYVAR

Horrors! Five subprocesses/subshell-environments and one temporary
file,
for nothing more than to feed one variable?! As for myself, when I
write
functions, at least when intended to be portable, I try to implement
them
without resorting to any fork/exec, if at all possible.

I'd at least collapse the grep/cut/head/xargs into one sed script.

=Brian


Farid Hamjavar

unread,
Sep 22, 2007, 8:20:31 PM9/22/07
to

Hello,

I am familiar with 'sa' and 'ac'

However, I like to know if any perl/shell script/C
utility out there that I can feed to it output from 'last'
and get some formatted output and/or statistics out of it.

Thank you,
Farid

Chris F.A. Johnson

unread,
Sep 23, 2007, 3:15:26 AM9/23/07
to
On 2007-09-23, Farid Hamjavar wrote:
>
> I am familiar with 'sa' and 'ac'

What are 'sa' and 'ac'? They are not standard utilities, so you
should explain what they are.

> However, I like to know if any perl/shell script/C
> utility out there that I can feed to it output from 'last'
> and get some formatted output and/or statistics out of it.

There are many ways of reformatting the output of last (or any other
command); what do you want to happen?

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
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

Maxwell Lol

unread,
Sep 23, 2007, 8:59:41 AM9/23/07
to
"Chris F.A. Johnson" <cfajo...@gmail.com> writes:

> On 2007-09-23, Farid Hamjavar wrote:
> >
> > I am familiar with 'sa' and 'ac'
>
> What are 'sa' and 'ac'? They are not standard utilities, so you
> should explain what they are.

sa is "system accounting" - used on a Solaris system. The solaris
newsgroup might offer more help.

John DuBois

unread,
Sep 23, 2007, 6:39:48 PM9/23/07
to
In article <Pine.LNX.4.64.07...@chishio.swcp.com>,

Farid Hamjavar <hamj...@swcp.com> wrote:
>However, I like to know if any perl/shell script/C
>utility out there that I can feed to it output from 'last'
>and get some formatted output and/or statistics out of it.

You could start with:

ftp://ftp.armory.com/pub/scripts/loginstat

John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

Farid Hamjavar

unread,
Sep 25, 2007, 11:43:59 AM9/25/07
to

Hello,
Many thanks to those who responded.

The 'ac' Connect accounting un*x and 'sa'
for resource (cpu ,etc) accounting of un*x
is on nearly all of linux flavors I came across
as well as AIX and Ultrix [for the old-timers
who tread this ;-)]

Same binaries/source offered via GNU ....
http://www.gnu.org/software/acct/manual/html_chapter/index.html

I think at this time I stick with what system offers.

Thanks,
Farid


Date: Sat, 22 Sep 2007 18:20:31 -0600 (MDT)
From: Farid Hamjavar <hamj...@swcp.com>
To: undisclosed-recipients: ;
Newsgroups: comp.unix.shell
Subject: last output


Hello,

I am familiar with 'sa' and 'ac'

However, I like to know if any perl/shell script/C


utility out there that I can feed to it output from 'last'
and get some formatted output and/or statistics out of it.

Thank you,
Farid

0 new messages