MaxV37
MaxV37

Reputation: 530

Program independent of its path

How do developers usually deal with different paths for executable files?

My program is currently in /usr/local/bin and I am wondering how to make it work weather it is in /usr/local/bin or in /usr/bin while being able to access the config files from one of the etc folders (depends on the executable path).

I can't just use relative paths because I need to make it relative to the path of the executable file and even then, it wouldn't be enough because I would need to access /etc weather than /usr/local/etc.

Is there a common way to deal with this situation? Is it dealt with during the installation? Do I need to make a different version of my program for the local and for the global path?

Upvotes: 0

Views: 89

Answers (2)

Jamil Said
Jamil Said

Reputation: 2093

If you run your program as root, then it should be able to access the configuration files on /etc/ or any other place without a problem. You could grep it from the script or whatever you need.

If your program is not run as root, then you should make sure that the configuration file being accessed on /etc/ gives the user the right to read it. See chmod man for more information.

Finally, your script should run fine from any of the locations you mentioned, although /usr/local/bin/ is generally where locally developed scripts should go. Just call your script by its full path and it will work: ex: /usr/local/bin/script

Note: don't forget to make your script executable: chmod +x /usr/local/bin/script

Upvotes: 0

megagrump
megagrump

Reputation: 128

In a shell script, you can detect the executable path of the script with

dirname `readlink -f $0` 

and work with that.

Upvotes: 1

Related Questions