- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- absolute path of a script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 07:44 PM
05-04-2004 07:44 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 07:47 PM
05-04-2004 07:47 PM
Re: absolute path of a script
VAR1=$PWD
Don't know this is what you are looking for!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 07:52 PM
05-04-2004 07:52 PM
Re: absolute path of a script
ABSOLUTE_PATH_OFCURRENT_SCRIPT=`which $0`
Regds,
Kapil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 07:55 PM
05-04-2004 07:55 PM
Re: absolute path of a script
SCRIPT=`basename $0`
VAR=`which $SCRIPT`
Regds,
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 07:56 PM
05-04-2004 07:56 PM
Re: absolute path of a script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 08:08 PM
05-04-2004 08:08 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 08:13 PM
05-04-2004 08:13 PM
Re: absolute path of a script
I think you migth try "which $0" though this isn't guaranteed to be correct either.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 08:18 PM
05-04-2004 08:18 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 08:20 PM
05-04-2004 08:20 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 08:33 PM
05-04-2004 08:33 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2004 09:04 PM
05-04-2004 09:04 PM
Re: absolute path of a script
$ 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:01 PM
05-05-2004 12:01 PM
Solution#!/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:24 PM
05-05-2004 12:24 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 06:10 PM
05-05-2004 06:10 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 12:39 AM
05-06-2004 12:39 AM
Re: absolute path of a script
FILENAME=${0##*/} #script name
DIRNAME=${0%/*} #calling path name
ABSPATH=`pwd` #absolute path
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 12:56 AM
05-06-2004 12:56 AM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 01:57 AM
05-06-2004 01:57 AM
Re: absolute path of a script
DIR=`pwd`
cd `dirname $0`
ABSPATH=`pwd`
cd $DIR
unset DIR
Marcel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 03:19 AM
05-06-2004 03:19 AM
Re: absolute path of a script
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 03:25 AM
05-06-2004 03:25 AM
Re: absolute path of a script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 03:46 AM
05-06-2004 03:46 AM
Re: absolute path of a script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 06:06 PM
05-06-2004 06:06 PM
Re: absolute path of a script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2004 07:29 PM
05-06-2004 07:29 PM
Re: absolute path of a script
I will keep them centralized using symbolic links.
Thanks for your help.
Claudio