LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   What directory am I in? (https://www.linuxquestions.org/questions/linux-general-1/what-directory-am-i-in-864596/)

bldrco 02-23-2011 01:56 PM

What directory am I in?
 
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?

Thanks!

MensaWater 02-23-2011 02:23 PM

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.)

bldrco 02-23-2011 02:45 PM

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.

MensaWater 02-24-2011 08:59 AM

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


bldrco 03-01-2011 09:36 PM

Quote:

Originally Posted by bldrco (Post 4268894)
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.

MensaWater 03-02-2011 07:55 AM

Glad you solved it and thanks for the link.

Can you go to thread tools mark the thread as SOLVED? That will help others who have similar issues in future find it quickly.


All times are GMT -5. The time now is 05:46 PM.