You are not logged in.

#1 2009-11-05 03:37:51

eirika
Member
From: New York
Registered: 2009-09-14
Posts: 65

[Solved]How can I get the name with full path of the running script?

This came into my mind when I was trying to write a simple backup script that also backups
itself. I don't know in which path would the script be run. So here is the question:

How can I get the file name of the running shell script as well as its full path?

If I start the script from the directory it is in then

$(pwd)/foobar.sh

will do the job, but the point is that I don't know where the script will be start from.

Last edited by eirika (2009-11-05 03:46:31)

Offline

#2 2009-11-05 03:38:55

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved]How can I get the name with full path of the running script?

readlink -f ?

I think will do for files.

In bash theres the $0 parameter that will give you the script's path/name.

Last edited by Gen2ly (2009-11-05 03:40:58)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#3 2009-11-05 03:41:40

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]How can I get the name with full path of the running script?

echo "$0"


This silver ladybug at line 28...

Offline

#4 2009-11-05 03:43:16

eirika
Member
From: New York
Registered: 2009-09-14
Posts: 65

Re: [Solved]How can I get the name with full path of the running script?

Thanks for the quick reply. You reminded me. I think

$(pwd)/$0

gets the jobs done. It's weird that when I first try $0 I didn't
catch the idea. smile

Last edited by eirika (2009-11-05 03:50:37)

Offline

#5 2009-11-05 03:51:03

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]How can I get the name with full path of the running script?

not `pwd`...
you don't need anything more than "$0" actually (and don't forget the quotes!)
but if you really want a fullpath, `readlink -f "$0"` seems really just for that (tacos to you, Gen2ly!)


This silver ladybug at line 28...

Offline

#6 2009-11-05 04:10:03

eirika
Member
From: New York
Registered: 2009-09-14
Posts: 65

Re: [Solved]How can I get the name with full path of the running script?

lolilolicon wrote:

not `pwd`...
you don't need anything more than "$0" actually (and don't forget the quotes!)
but if you really want a fullpath, `readlink -f "$0"` seems really just for that (tacos to you, Gen2ly!)

I tried readlink and it works great. But I feel confused about the rest of your words.
Maybe you mean if I symlink the script and run from the symlink then I have to dereference $0 to
get the actual script?

Offline

#7 2009-11-05 04:33:08

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]How can I get the name with full path of the running script?

@Gen2ly
figured i'd thank you again Gen2ly big_smile
i've always used this rel2abs function below, guess i'll be using readlink from now on

@eirika, i did not mean symlinks but that `pwd` might go wrong. the reason why i think "$0" will be enough was not correct. There's more i need to consider. well, just look at the script and result below.

This is a test script:

rel2abs () {
    echo "$(cd "$(dirname "$1")" ; pwd)/$(basename "$1")"
}

cd ..

ls "`echo "$0"`"
ls "`readlink -f "$0"`"
ls "`rel2abs "$0"`"
ls "`echo "$(pwd)/$0"`"

The result:

$ pwd
/home/me/code/a/b
$ bash test.sh
ls: cannot access test.sh: No such file or directory
ls: cannot access /home/me/code/a/test.sh: No such file or directory
ls: cannot access /home/me/code/a/test.sh: No such file or directory
ls: cannot access /home/me/code/a/test.sh: No such file or directory
$ mv test.sh ..
$ bash ../test.sh
ls: cannot access ../test.sh: No such file or directory
ls: cannot access /home/me/code/test.sh: No such file or directory
ls: cannot access /home/me/code/test.sh: No such file or directory
ls: cannot access /home/me/code/a/../test.sh: No such file or directory

Everything fails (!)
So my suggestion is get the absolute path in the beginning of the script (without doing any `cd` before it), using readlink.

I'll have to think it over a bit more

-- Edit --
of course (...) we should do relative-path-to-absolute-path conversion before any `cd`. now it seems _logical_ to me any of the methods above is bound to fail if it's done after `cd`.
and `$(pwd)/$0` is equivalently correct as readlink or rel2abs, just with a twist (like "/home/me/a/b/../test.sh")

-- Edit 2 --
yep, `$(pwd)/$0` is less correct than readlink/rel2abs (like "/home/me/a/b//bin/test.sh"), according to tavianator

Last edited by lolilolicon (2009-11-05 06:12:27)


This silver ladybug at line 28...

Offline

#8 2009-11-05 05:05:59

eirika
Member
From: New York
Registered: 2009-09-14
Posts: 65

Re: [Solved]How can I get the name with full path of the running script?

@lolilolicon:

It seems to me that your script fails just because you changed the working directory in the script.
So if I conbine "$(pwd)" with "$0" at the beginning of the script I think it still works.

And if you change the working dir somewhere, then even readlink won't do the job anymore
because now we lose track of the location.

Anyway the major problem has been solved, the rest I think is just careful programming. Thank you all. smile

Offline

#9 2009-11-05 05:18:07

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved]How can I get the name with full path of the running script?

eirika, you are right. i guess i messed things up then cleared it up for myself ::lol::

*goes to edit my careless scripts*


This silver ladybug at line 28...

Offline

#10 2009-11-05 05:48:03

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: [Solved]How can I get the name with full path of the running script?

If someone specifies your script with a full path, or if your script is in someone's $PATH, "$(pwd)/$0" will be wrong.  I think what you want would be like this:

#!/bin/sh

# Must be before you cd
SELF=$(readlink -f "$0")

or this:

#!/bin/sh

# Must be before you cd
SAVEDPWD=$(pwd)

cd somewherelse

SELF=$(cd "$SAVEDPWD" && readlink -f "$0")

Offline

#11 2009-11-05 14:12:37

eirika
Member
From: New York
Registered: 2009-09-14
Posts: 65

Re: [Solved]How can I get the name with full path of the running script?

tavianator wrote:

If someone specifies your script with a full path, or if your script is in someone's $PATH, "$(pwd)/$0" will be wrong.

That's true. It never came to my mind. Now I feel like that I have to check every line I wrote down.

Offline

#12 2009-11-05 15:24:46

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved]How can I get the name with full path of the running script?

lolilolicon wrote:
rel2abs () {
    echo "$(cd "$(dirname "$1")" ; pwd)/$(basename "$1")"
}
...

Nice.  I was trying to think of a way to do this before I learned about readlink.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

Board footer

Powered by FluxBB