Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-09-2006, 01:59 PM   #1
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545

Rep: Reputation: 30
Question about path specification in program/script code

Here's the thing: I have several programs and scripts in a software distribution that I am building, several of which call one another in very elegant arrangements. Lets assume I only have one program and one script in this package. Lets also assume:

My package is in:

My program is in:

My script is in:

How can I make sure that if a user downloads a tarball of my package that prog and scr can find each other, while allowing the user to run either from anywhere?

If I use relative paths (ie, ../scripts/src, or ../programs/prog), then it only works if the user invokes prog or script in the directory in which they are located. If I specify an absolute path, then the user has to install pkg into the directory I specify.

1) I don't want to force the user to do a "make install". That should not be necessary with this tool.
2) I don't think I want to require them to add the directories for prog/scr to their path either, or make a symbolic link...

Maybe this is something trivial in UNIX that I don't know about, but I'm stumped.
Old 05-09-2006, 10:13 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.7, Centos 5.10
Posts: 16,923

Rep: Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215
There's no magic in a computer; somewhere you'll have to specify how to find something.
Either add your main dir to their path or create a symbolic link in eg /usr/bin that pts to your dir.
You could (but not recommended) add the functionality of the 'find' cmd (man find) to your prog/script so they can search for each other.... only works if there's only one copy of each.
Old 05-11-2006, 05:07 AM   #3
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi R00ts,

As chrism01 said, there's no magic in a computer; all that is hard work. As you said you have a predefined tree, you can change your PATH environment at the beginning of each program or script before any calling. Doing this, all the programs and scripts will be found in spite of the process of user call; for instance, in a shell script, you could do this:
#Save current environment path
# Save the current directory, from where the user
# is calling the script
# get the name as the user invoked this script:
# it may be ./myscript or,
# /usr/bin/pkg/scripts/myscript or,
# ./pkg/scripts/myscript or,
# ../pkg/scripts/myscript or ....
# Extract the directory part of the name; it may be a "."
# or "/usr/bin/pkg/scripts" or ./pkg/scripts or ...
MY_DIR=`dirname $ME`
# Go to that directory
cd $MY_DIR
# Get the complete directory. Now it will always be an
# absolute directory
# Restore the current user directory
# Extract the base directory, where your package is installed.
# Assemble the path needed to run your package
# Look into the path to see if you already did the changes
if [ `echo $OLD_PATH | grep -c $MY_PATH` -eq 0 ]
  # If not, export another path containing your directories
  # in the beginning so your software will have search priority
# Now, this particular $PATH will be set for the execution of all 
# programs and scripts called from this script; you don't even
# need to use "../script" or "../program" to invoke them
The same can be done on the programs. The logic will be approximately the same.



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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
script to change unix path to windows path in all files csross Programming 8 04-29-2006 02:05 PM
How to make a path for a program? Tommi Linux - Software 12 06-19-2004 06:38 PM
Design specification for Path MTU Discovery myself_rajat Linux - Networking 0 06-02-2004 04:00 AM
Path to system settings program!?! Punkduplo Red Hat 4 10-25-2003 05:41 AM
How to make a path for a program? Tommi Linux - General 1 06-17-2003 06:43 AM

All times are GMT -5. The time now is 08:58 AM.

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