Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-12-2008, 09:09 PM
|
#1
|
|
LQ Newbie
Registered: May 2006
Location: Boston, MA
Distribution: Gentoo
Posts: 29
Rep:
|
how can a shell script determine its own location when being sourced?
I have a setup.sh script that needs to be sourced, not executed. What can I use inside this script to determine the full path to itself, so I can compute some variables relative to it?
If it was always sourced in the same directory as it resides (not an option) I would of course use "$PWD". If it was being executed, I would use "$0". If it was a python script, I could also use __file__. Etc. But I can't think of a solution for the basic situation here.
Thanks in advance for any help.
|
|
|
|
02-13-2008, 09:18 PM
|
#2
|
|
Senior Member
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495
Rep:
|
How about putting
cmdline=`history 1`
at the start of the script? Then $cmdline gives the commandline for the source command.
|
|
|
|
02-15-2008, 04:12 PM
|
#3
|
|
LQ Newbie
Registered: May 2006
Location: Boston, MA
Distribution: Gentoo
Posts: 29
Original Poster
Rep:
|
Thanks for the help. I never thought of that, and I'm definitely going to use it in some of my scripts.
However, I hesitate to rely on it in production releases. That variable will be the whole command line, including any other commands composed with it using `;', and the very problem I'm having is causing people to do that very often (cd $location; source ./setup.sh; cd $OLDPWD).
Splitting on `;' would be easy enough, but there are lots of other shell metacharacters that could come into play. Though unlikely, the command could be used in conjunction with commands like time and watch, and environment variables are still sitting there unevaluated.
For example,
Code:
time { source "$user_defined_variable"/setup.sh; myprogram; }
Even if I could write the code to determine that path to setup.sh in this case, I would not trust it in general.
|
|
|
|
02-15-2008, 10:59 PM
|
#4
|
|
Senior Member
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495
Rep:
|
Yes, I thought it was a bit kludgy, but it seemed like the file had already been closed by the time the commands started to execute, so in that sense the location of the file really was just 'history'.
I had another look at the man page for bash. Have you looked at the BASH_SOURCE variable? It seems to be giving what you want inside the script.
|
|
|
|
02-19-2008, 12:51 PM
|
#5
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
${BASH_SOURCE} should be just what you're asking for.
${BASH_SOURCE%/*} should give you the path you need.
${BASH_SOURCE##*/} would give just the actual name of the file, just in case you need to know the name it was called by.
BTW, what did you do/use?
Refs
In the bash man page, which is long & sometimes confusing, search for: - BASH_SOURCE
- ${parameter#word}
|
|
|
|
02-21-2008, 01:57 PM
|
#6
|
|
LQ Newbie
Registered: May 2006
Location: Boston, MA
Distribution: Gentoo
Posts: 29
Original Poster
Rep:
|
Thanks a lot! BASH_SOURCE is exactly what I was looking for.
Unfortunately it seems to not be available before bash version 3, and I would like a solution for csh, too, but I can deal with those situations as special cases.
For the vast majority of users, problem solved.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:23 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|