Operating System - HP-UX
1832858 Members
3481 Online
110048 Solutions
New Discussion

Re: absolute path of a script

 
SOLVED
Go to solution
Claudio Cilloni
Honored Contributor

absolute path of a script

hi all.

I have got a question for you:

I need to know, inside a shell script, the absolute full path of the script itself.

example:
the script is located in /home/user/scripts/script.sh .

I need a way to get the full string "/home/user/scripts/script.sh" in a variable named ABSPATH independently how I run the script:

$ /home/user/scripts/script.sh

or

$ cd /home/user/scripts/
$ ./script.sh

or

$ cd /home/user/scripts/otherdir
$ ../script.sh

HP-UX 11.

thanks all,
Claudio
21 REPLIES 21
Bharat Katkar
Honored Contributor

Re: absolute path of a script

what about adding this variable in your script:

VAR1=$PWD

Don't know this is what you are looking for!!!
You need to know a lot to actually know how little you know
KapilRaj
Honored Contributor

Re: absolute path of a script

Inside the script ?

ABSOLUTE_PATH_OFCURRENT_SCRIPT=`which $0`

Regds,

Kapil
Nothing is impossible
KapilRaj
Honored Contributor

Re: absolute path of a script

Or a batter one would be

SCRIPT=`basename $0`
VAR=`which $SCRIPT`

Regds,

Kaps
Nothing is impossible
Bharat Katkar
Honored Contributor

Re: absolute path of a script

Claudio,
Sorry i did not interpreted it properly. Okay now i have one solution for you.
I tested this on POSIX shell and working fine.

ABSPATH=`find / -name script.sh -print`

Remember i have used back quotes there. But only problem is it will take some time to scan everything.



You need to know a lot to actually know how little you know
KapilRaj
Honored Contributor

Re: absolute path of a script

Bharat,

Sorry to interrupt you but obv it will help u as a Sysadmin. Your method would list all such script's present in his system

i.e.
$HOME/script/script as well as
/tmp/script
which is not what he needs ....

What he wants to get is the path of the script which he is currently running.

From his statements we can find that the script is available in his PATH variable and by doing a "whence" or "which" you can locate his script's absolute path.

Do not misunderstand me , I just wanted to make it clear to you , after all we r friends in here ... Good time :)

Regds,

Kaps
Nothing is impossible
Mark Grant
Honored Contributor

Re: absolute path of a script

$0 doesn't give you the absolute path to the script. It only gives the name it was called as. So, if you called it as "./script", that will be $0.

I think you migth try "which $0" though this isn't guaranteed to be correct either.
Never preceed any demonstration with anything more predictive than "watch this"
Claudio Cilloni
Honored Contributor

Re: absolute path of a script

sorry, but the $PWD and the which command give me the relative path of
my script.

the 'find' solution of Bharat works, but the full tree scan made by the find
command is too heavy for my needs. thanks anyway.

there is a way (or a command) to intelligently join the values of the
variables $PWD and $0? I think this could be the solution.

thanks again for your help.

Claudio
Bharat Katkar
Honored Contributor

Re: absolute path of a script

Thanks Kapil,
You are obsulotely right... See my objective is to learn more and more with people like U and there are very few one's who really interrupt and spend some time to make things clearer. Thanks a lot YAAR.
And one more thing kapil, do not tell anybody but since you called me friend let me tell that i am bit .. in scripting. :))))

WIth regards,
Bharat
You need to know a lot to actually know how little you know
KapilRaj
Honored Contributor

Re: absolute path of a script

I am surprised to see PWD worked for you !! pwd will show you the path where u r executing it from !

In ksh "which" works for me .. You may also try with "whence"

i.e.

SCR=`basename $0` # Does this work for u
whence $SCR


Regds,

Kaps
Nothing is impossible
Claudio Cilloni
Honored Contributor

Re: absolute path of a script

whence $0 seems to work:

$ cd /home/user/scripts/otherdir
$ ../script.sh

inside the script.sh whence $0 gives /home/user/script/otherdir/../script.sh

this isn't exactly what I'm looking for, but could be enough. Thanks Kapil.

Ciao!
Claudio
Duncan Galbraith
Frequent Advisor
Solution

Re: absolute path of a script

In ksh this should give you exactly what you want:

#!/usr/bin/ksh

cd $(dirname $0)
myScript=$(echo "$PWD/$(basename $0)" | sed 's!//!/!')
cd - 2>/dev/null

echo "My script is $myScript"


The little bit of sed is just there to cover the case where your script is in the root directory.
Patrick Wallek
Honored Contributor

Re: absolute path of a script

I know of no DEFINITE way to do this.

whence will ONLY work if the directory the script resides in is in your PATH environment variable.

If the script you are executing will only reside in a single directory why not just manually code the directory into a variable?

DIR=/path/to/script.sh
Jeroen Peereboom
Honored Contributor

Re: absolute path of a script

mcClaudio,

nice problem. I don't now an easy solution yet.

As you may know, $0 is the name 'as the script is called'.
Examples are:
1. /home/user/scripts/script.sh
2. ../../scripts/script.sh
3. ./script.sh
4. ./scripts/script.sh
5. $SCRIPTS/script.sh

1 is easy, just $0.
2 - 4 give paths relative to the current directory ($PWD) (at the time the script is called).
5 may be an absolute or relative path, depending on $SCRIPTS, and will not be discussed further, since the shell will expand the variable $SCRIPTS before execution.

After this analysis, I discover how clever Duncan solves the problem of the relative paths by using 'cd $(dirname $0)'. This eliminates the relative path 'things' (. and ..) from the strings. He prevents editing problems like: how do I change '/home/user/b/../scripts/script.sh' into '/home/user/scripts/script.sh'.

I would use his solution.

JP
David Nardi
Advisor

Re: absolute path of a script

How about setting variable as such?

FILENAME=${0##*/} #script name
DIRNAME=${0%/*} #calling path name
ABSPATH=`pwd` #absolute path

Trond Haugen
Honored Contributor

Re: absolute path of a script

What do you need the path for? You question may be easier to answer if you tell us that.

Any way of finding it my end up finding another file/script by the same name. So my thought was that you could "hardcode" it in the script. But that is probably not what you want.

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Marcel Kuiper
Honored Contributor

Re: absolute path of a script

This seems to work

DIR=`pwd`
cd `dirname $0`
ABSPATH=`pwd`
cd $DIR
unset DIR

Marcel

If we can't fix it, it ain't broken
Claudio Cilloni
Honored Contributor

Re: absolute path of a script

I'm writing a set of scripts to do some DBA activities on various Oracle
instances, and I want the script read the ORACLE_SID value from the
name of the path into it resides. So I can copy these scripts under the base
directory of each instance and they know, without adding parameters or hardcoded
variables, the ORACLE_SID to use.
I want to be able to run these scripts with the full path (for example
scheduled via cron) and by hand without worring about the path.
It is good for some colleagues thinking that a script that is located under the
instance's base directory work on that instance.

Marcel's idea seems the final solution I need. thanks!

Ciao
Claudio
Mark Grant
Honored Contributor

Re: absolute path of a script

Well then maybe /etc/oratab might be a useful thing. Something like the following.

If $0 contains a path i.e not ./<script> or just <script> then get the part of the path before the script name and look it up in /etc/oratab and get the oracle SID. If the $0 is one of the two mentioned above then simply use `pwd`, get the last bit of the path returned and look that up in /etc/oratab.

Of course, I might not of understood the question properly. That happens frequently.
Never preceed any demonstration with anything more predictive than "watch this"
Patrick Wallek
Honored Contributor

Re: absolute path of a script

Having copies of the script in different directories sounds like a pain if you have to modify them all.

Why not add the ORACLE_SID as a parameter to the script? When you run the script you do it like:

# /path/to/script.sh oracle_sid

If you don't specify a SID, you generate an error.

That would make a whole lot more sense to me.
Trond Haugen
Honored Contributor

Re: absolute path of a script

I see you point Claudio and for cron jobs that would work nicely as the script will always be started with the absolute path.

Patrick has a point but if you have ONE cental script that will do the actual job and theise other scripts just start that with the right ORACLE_SID it should be easier to administrate.

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Claudio Cilloni
Honored Contributor

Re: absolute path of a script

I agree with you, guys, but other people like this way to run these scripts.
I will keep them centralized using symbolic links.

Thanks for your help.

Claudio