# 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.
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.
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
/s.
On Sep 17, 12:28 am, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
The resolution of the pathname in $0, and its normalization, via
symlinks
and otherwise, is addressed in my script "resolvepath" below:
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
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
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
$ 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
:)
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
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
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
> 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.
You could start with:
ftp://ftp.armory.com/pub/scripts/loginstat
John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
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