Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have an environmental file that needs to export a variable that indicates the absolute path that the environmental file is in. Is this possible? If it were an executable script, something like
dirname `readlink -f $0`
would do the trick since it is executed, but this file is being sourced instead. The file can be sourced from anywhere, so using `pwd` will not work either. Is there something else I can do in the file to figure out where it is located?
How are you sourcing the file if you don't know what directory it is in?
That is to say it seems the step in your script that tries to source the file must already know where it is in which case you could set the variable before the sourcing. Sourcing a file won't get rid of earlier variables. (Of course if the sourced file is setting variables that were previously set it will of course replace those.)
Sounds odd, I know. My question might be misleading so let me explain what I'm really trying to accomplish and maybe there's a better way:
This is for a fairly complex Makefile hierarchy. At the top level, the Makefile does an "include .config", where .config has all my variables. One variable here says where the output needs to go and is relative to the top level, and it has to be an absolute path. As I go down the hierarchy, if these variables are not set, the Makefile at that level will include a ".config" at the level above it that simply includes the one above that until it gets to the top. So I'd to run "make" at any level in the system and have the output in the same directory from the top, if that makes sense.
OK but here again why can't you set the variable in the routine that is going up the levels rather than within the sourced file?
Say you were in /the/deepest/directory/level/ever/found/on/this/system. You don't find .config in /the/deepest/directory/level/ever/found/on/this/system so you cd .. and look in /the/deepest/directory/level/ever/found/on/this. You don't find it there so you cd .. and look in /the/deepest/directory/level/ever/found/on etc... Why can't you set the variable at the point you go back a directory rather than from within the source .config file?
e.g.
Code:
CONFIGDIR=unknown
until [ $CONFIGDIR != "unknown" ]
do
if [ -f .config ]
then CONFIGDIR=$(pwd)
fi
done
This is for a fairly complex Makefile hierarchy. At the top level, the Makefile does an "include .config", where .config has all my variables. One variable here says where the output needs to go and is relative to the top level, and it has to be an absolute path. As I go down the hierarchy, if these variables are not set, the Makefile at that level will include a ".config" at the level above it that simply includes the one above that until it gets to the top. So I'd to run "make" at any level in the system and have the output in the same directory from the top, if that makes sense.
To answer my own question, I discovered what I'm looking for is called "non-recursive make" (http://www.xs4all.nl/~evbergen/nonrecursive-make.html). New concept to me, but probably familiar to experienced build engineers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.