#!/usr/bin/env bash echo " \\$0: $0" _my_name=`basename $0` if [ "`echo $0 | cut -c1`" = "/" ]; then _my_path=`dirname $0` else _my_path=`pwd`/`echo $0 | sed -e s/$_my_name//` fi echo " Filename: $_my_name" echo "Absolute path: $_my_path"
Recent Comments
Recent Posts
Tags
activerecord
Apache
backyardigans
Bash
blackberry
buzz lightyear
children
curious george
currency
dora the explorer
dpkg
go diego go
kids
little bill
mac
max & ruby
mtnwestrubyconf
mwrc09
nick jr
ni hao kai-lan
noggin
OS X
passenger
pbs kids
phusion
pinky dinky doo
playhouse disney
presentation
Rails
Ruby
ruby enterprise edition
ruby on rails
Security
starfall
stock quote
Subversion
tether
tips
Ubuntu
usability
warehouse
wonder pets
wow wow wubbzy
yahoo
yo gabba gabba
you could just use the “basename” utility to get the filename, e..g
basename /foo/bar/test.txt
returns:
test.txt
That’s what I’m doing to get the file name, here:
_my_name=`basename $0`
The real point is to get the absolute path of the directory containing the script.
If you are familiar with Ruby, you can accomplish the same thing with File.dirname(__FILE__)
Are you sure this works, using this exactly like in your sample of centos 5.3
Yeah, just to make sure:
[jtanium@flobot ~]$ cat script_name.sh #!/usr/bin/env bash echo " \\$0: $0" _my_name=`basename $0` if [ "`echo $0 | cut -c1`" = "/" ]; then _my_path=`dirname $0` else _my_path=`pwd`/`echo $0 | sed -e s/$_my_name//` fi echo " Filename: $_my_name" echo "Absolute path: $_my_path" [jtanium@flobot ~]$ ./script_name.sh \./script_name.sh: ./script_name.sh Filename: script_name.sh Absolute path: /home/jtanium/./ [jtanium@flobot ~]$It does make me wonder what the deal is the the first echo $0 thing is…
“$0″ is the name of the script file. It’s not by default an absolute path.
With respect, this is not a clean solution; readlink as in http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script/ is much better. The style is also not very good and breaks with spaces (the link above does too; it’s not my example). Try (with outer quotes)
“$(readlink -f “$0″)”
“$(dirname “$(readlink -f “$0″)”)”
Despite what it looks, the inside of $() seems to start some sort of new environment, so the quotes around the “$0″ behave like you’d expect them to.
regards,
Nicholas
@nicholas,
That’s pretty sweet. I’ve never seen that before, and I’ll definitely use it.
Thanks.
in bash you can also use the following to get the path of the script.
BaseDir=$(dirname $(which ${0}))
Bear in mind, that is only going to work if the script is in your path. Me, I’m not going to trust that.
Speaking of bash, you could do it like this:
pushd $(dirnamr $0) > /dev/null
my_path=$(pwd)
popd > /dev/null > /dev/null
You could do much the same with cd (and cd -), but I don’t remember if ‘cd -’ is any more portable than pushd. Much the same with just remembering the ‘old pwd’.
It has a very nice feature of removing junk from the file name, like when you call your script with something like
../../one/dir/../../..///another/dir/../../../../usr/bin/script.sh
which might result from strange path concatenation (it *does* happen).
Before you ‘scream’, yes, I know, this method assumes, that the executor has +x for the directory containing the script. Oh well.. Show me any sane person, who removes x from directories, but permits execution scripts therein