LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-23-2011, 01:56 PM   #1
bldrco
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Rep: Reputation: 1
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!
 
Old 02-23-2011, 02:23 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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.)
 
Old 02-23-2011, 02:45 PM   #3
bldrco
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 1
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.
 
Old 02-24-2011, 08:59 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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
 
Old 03-01-2011, 09:36 PM   #5
bldrco
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by bldrco View Post
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.
 
1 members found this post helpful.
Old 03-02-2011, 07:55 AM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Username & Password Sync Fedora Directory and Microsoft Active Directory karnac01 Fedora 4 07-19-2010 12:51 AM
change default vsftp anonymous accout directory /var/ftp to other directory? hocheetiong Linux - Newbie 2 01-24-2010 06:33 PM
LXer: Directory services made easy with Fedora Directory Server LXer Syndicated Linux News 0 12-08-2006 03:54 AM
How to protect a file/directory in your home login directory (RH Linux) jitsenho Linux - Security 9 07-03-2006 11:08 PM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration