Script sourced by bash -- can it determine it"s own location?
Script sourced by bash -- can it determine it"s own location?
am 17.09.2007 01:24:11 von google
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.
Re: Script sourced by bash -- can it determine it"s own location?
am 17.09.2007 05:05:07 von Icarus Sparry
On Sun, 16 Sep 2007 16:24:11 -0700, google 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=???
>
> 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.
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.
Re: Script sourced by bash -- can it determine it"s own location?
am 17.09.2007 06:09:52 von Cyrus Kriticos
google@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:
export MYLOCATION=${BASH_ARGV[0]%/*}
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Script sourced by bash -- can it determine it"s own location?
am 17.09.2007 06:28:32 von Cyrus Kriticos
google@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
Re: Script sourced by bash -- can it determine it"s own location?
am 17.09.2007 15:07:47 von google
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
wrote:
> 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
Re: Script sourced by bash -- can it determine it"s own location?
am 18.09.2007 02:10:40 von brian_hiles
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=%22functi on+resolvepath%22+group%3Acomp.unix.shell&rnum=2&hl=en#83b2b 4fef10d3717
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
Re: Script sourced by bash -- can it determine it"s own location?
am 18.09.2007 09:05:10 von Stephane CHAZELAS
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
Re: Script sourced by bash -- can it determine it"s own location?
am 20.09.2007 02:10:18 von brian_hiles
On Sep 18, 12:05 am, Stephane CHAZELAS
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=E9phane. 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=3D'SOURCE_NAME=3Dfoo 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=3D'SOURCE_NAME=3D$f $f'"
done
=3DBrian
Re: Script sourced by bash -- can it determine it"s own location?
am 20.09.2007 02:41:10 von Tiago Peczenyj
$ 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
:)
On 19 set, 21:10, bsh wrote:
> On Sep 18, 12:05 am, Stephane CHAZELAS
> 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=E9phane. 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=3D'SOURCE_NAME=3Dfoo 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=3D'SOURCE_NAME=3D$f $f'"
> done
>
> =3DBrian
Re: Script sourced by bash -- can it determine it"s own location?
am 20.09.2007 23:50:01 von brian_hiles
Tiago Peczenyj 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
last output
am 23.09.2007 02:20:31 von Farid Hamjavar
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
Re: last output
am 23.09.2007 09:15:26 von cfajohnson
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
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: last output
am 23.09.2007 14:59:41 von Maxwell Lol
"Chris F.A. Johnson" 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.
Re: last output
am 24.09.2007 00:39:48 von spcecdt
In article ,
Farid Hamjavar 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 spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Re: last output
am 25.09.2007 17:43:59 von Farid Hamjavar
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.h tml
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
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