LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-09-2006, 12:59 PM   #1
R00ts
Member
 
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:
/home/me/dev/pkg/

My program is in:
/home/me/dev/pkg/programs/prog

My script is in:
/home/me/dev/pkg/scripts/scr


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, 09:13 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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, 04:07 AM   #3
osvaldomarques
Member
 
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:
Code:
#Save current environment path
OLD_PATH=$PATH
# Save the current directory, from where the user
# is calling the script
USER_CURRENT_DIR=`pwd`
# 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 ....
ME=$0
#
# 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
MY_DIR=`pwd`
# Restore the current user directory
cd $USER_CURRENT_DIR
# Extract the base directory, where your package is installed.
MY_PACKAGE_DIR=`dirname $MY_DIR`
# Assemble the path needed to run your package
MY_PATH="MY_PACKAGE_DIR/program:MY_PACKAGE_DIR/scripts"
# Look into the path to see if you already did the changes
if [ `echo $OLD_PATH | grep -c $MY_PATH` -eq 0 ]
then
  # If not, export another path containing your directories
  # in the beginning so your software will have search priority
  export PATH=$MY_PATH:$OLD_PATH
fi
#
# 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.

Osvaldo
 
  


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 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 01:05 PM
How to make a path for a program? Tommi Linux - Software 12 06-19-2004 05:38 PM
Design specification for Path MTU Discovery myself_rajat Linux - Networking 0 06-02-2004 03:00 AM
Path to system settings program!?! Punkduplo Red Hat 4 10-25-2003 04:41 AM
How to make a path for a program? Tommi Linux - General 1 06-17-2003 05:43 AM


All times are GMT -5. The time now is 12:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration