![]() |
I have a further requirement. The last few scripts suggested do indeed give the location of the script as it runs.
However if you source the script instead of running it, this gives the current working directory from which you called the script, not the directory in which the script itself resides. Here's the scenario: I have a number of slightly different versions of a project in different folders in my home directory, which I want to compile intermittently. Each version contains certain libraries and output folders which have been hard-coded into the build process (not by me) to be located using environment variables. What I want is to be able to have the same script sit in the top level folder of each of these different project versions, set the relevant paths for that version, relative to that top directory, and add them to the environment before I go to build. Ya dig? Given that I need to source the script instead of having it run in its own execution environment, it seems as if the solutions suggested here fall just short of sufficient (although they are as thorough as any I have seen on this topic). Any further ideas would be gratefully received :-) |
New problem, new thread.
|
I think the following should find a scripts directory path:
path=$(cd ${0%/*} && pwd -P) |
Here is a function I regularly use to change the directory to wherever the script resides.
Code:
WorkDir () |
Quote:
However... i found an (admittedly) obscure way to "break" them all. :) If a symlink is created to the script, and we call upon that symlink... then the parent we wind up with turns out to be the symlink's parent. (maybe that's acceptable on occasion, but probably not in most cases). Adding one more line (and one more variable) handles that potential symlinked scenario... Code:
|
I dug a ton of information and found a simple solution:
Code:
#!/bin/bash |
Quote:
$ cd ~/Desktop $ ../bin/pth ../bin # ^ not an absolute pathname, so... not exactly a sufficient solution. [also doesn't pass the symlink test (see previous post).] |
If you aren't allergic to tcsh, there it is simply $0:h
(More such modifiers are listed on the tcsh man page in the "history substitution" section.) Just as a note, the original poster's application (like many others) doesn't care whether it's a full or relative path, and if he is using this approach in several directories, he would actually be wise to use symbolic links to a single updatable, replaceable script, in which case it should indeed use the directory containing the link, not the directory of the final script. The following works on both Linux and OS X, when a script needs to be in its own directory. Code:
#!/bin/tcsh |
Quote:
>>> Can a Bash script determine where it is? Quote:
Quote:
Quote:
|
The real tricky part is that, because of links (both hard and soft), the concept of "THE name of a file" is a flawed concept. A script can no more tell where it "really is" than where any other file "really" is.
The best it can do is ask what path whoever launched it used to find it. If that path is relative, well, that's what you've got. |
I don't have a Mac but I found this page while looking for the same. This works with Linux. Guess it works on a Mac too.
Code:
#! /bin/bash |
Quote:
|
| All times are GMT -5. The time now is 05:42 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.