- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How do I script the directory in which this 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
Discussions
Discussions
Forums
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
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
тАО12-22-2004 07:00 AM
тАО12-22-2004 07:00 AM
How do I script the directory in which this script is located?
They can be in:
/usr/local/bin/
/opt/local/bin/
/usr/contrib/bin
How can I script definitively where they are from within the script. You can't just go $PWD, because if that will give you where you are sitting right now. You can't go "whence" because that may turn up more than one of them. Also, the location may be some other place not in your path.
I think that you can use some combination of "basename" or "dirname" or something like that.
- Tags:
- directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 07:04 AM
тАО12-22-2004 07:04 AM
Re: How do I script the directory in which this script is located?
which $0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 07:15 AM
тАО12-22-2004 07:15 AM
Re: How do I script the directory in which this script is located?
dirname `which $0`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 07:46 AM
тАО12-22-2004 07:46 AM
Re: How do I script the directory in which this script is located?
Since I'm a ksh user, it doesn't work for me. It says "no xxxx in and lists my path (correctly - but it can't find xxxx which is right there in the path.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 07:50 AM
тАО12-22-2004 07:50 AM
Re: How do I script the directory in which this script is located?
# whence xxxx
- Tags:
- whence
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 07:53 AM
тАО12-22-2004 07:53 AM
Re: How do I script the directory in which this script is located?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 10:39 AM
тАО12-22-2004 10:39 AM
Re: How do I script the directory in which this script is located?
program="${0:##*/}"
path="${0:%/*}"
for posix type shells
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 11:51 AM
тАО12-22-2004 11:51 AM
Re: How do I script the directory in which this script is located?
path="${0:%/*}"
#
# find where I am, absolute path to this directory
#
oldDir=$(pwd)
cd $path
mydir=$(pwd)
cd $oldDir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 03:23 PM
тАО12-22-2004 03:23 PM
Re: How do I script the directory in which this script is located?
You'll need to test the dirname results. If it starts with /, then you have a full pathname and you know where it came from. If dirname is empty or contains relative locations, then the location can be determined with the construct:
MYNAME=${0##*/}
MYDIR=$(dirname $0)
if [ $(echo $MYDIR | cut -c 1) != / ]
then
MYFULLPATH=$(whence ./$MYDIR/$MYNAME)
else
MYFULLPATH=$MYDIR/$MYNAME
fi
echo $MYFULLPATH
In this snippet of code, we use the shell to perform basename, but use dirname rather than ${0:%/*}. The reason is that if the script is started by the simple name, the shell construct returns the basename rather than . so we use dirname to make sure it is really a valid directory reference.
The way it works is to put ./ in front of whatever dirname is found in $0. Then run the ./DIRNAME/SCRIPTNAME through whence and it returns a useable full pathname. Note that you may see an extra ./ in $MYFULLPATH but it is still a valid directory reference.
This sample has been tested with:
/tmp/myscript
myscript
./myscript
../myscript
../bin/myscript
./././myscript
and so on. It should identify the full pathname of the script regardless of how it was invoked.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2004 11:17 PM
тАО12-22-2004 11:17 PM
Re: How do I script the directory in which this script is located?
MYNAME=$(cd $(dirname $0); pwd -P)/${0##*/}
Best regards...
Dietmar.