File paths on Linux
Config/init files exist for a reason. Store the path in there (config file at absolute location like /usr/share/app-name/defaults.conf, so always easy to locate).
quote: Original post by Oluseyi
Config/init files exist for a reason. Store the path in there (config file at absolute location like /usr/share/app-name/defaults.conf, so always easy to locate).
Tsk, tsk... what about when the program ends up in /usr/local/share/app-name, or /opt/app-name? Hardcoding paths into an executable is almost always a bad idea.
...but....
Hmm, what if the installer script modified the executable to store the actual target directory? it'd just have to know the location of the path in the executable's string table, which would previously be all nulls.
echo $TARGET_DIR | dd of=$TARGET_DIR/bin/myprog bs=1 skip=823487 count=127
How appropriate. You fight like a cow.
[edited by - sneftel on April 9, 2003 4:23:15 AM]
You can use environment variables or configuration files as was mentioned earlier.
Most programs will work with assumed defaults if an environment variable isn''t set in the invoking shell or altered with command line syntax.
So you could for example assume ''/home''. You can use ''environ'' (man environ) to get all the environment variables and check for the one your program would use. So a user could just set the default directory for your application by setting a shell variable to override the default.
Then just give users instructions that the application needs to have that environment variable set to run properly, and of course, gracefully handle the case where the default is incorrect and the user fails to set a shell variable or a command line run option.
Interim.
Most programs will work with assumed defaults if an environment variable isn''t set in the invoking shell or altered with command line syntax.
So you could for example assume ''/home''. You can use ''environ'' (man environ) to get all the environment variables and check for the one your program would use. So a user could just set the default directory for your application by setting a shell variable to override the default.
Then just give users instructions that the application needs to have that environment variable set to run properly, and of course, gracefully handle the case where the default is incorrect and the user fails to set a shell variable or a command line run option.
Interim.
Then use environment variables or command line switches and avoid the default.
If the environment variable isn''t set or the CLI switch isn''t set, then kick out an error message telling users they must set the path to whatever resource you want to use.
You can try to stick with something like /usr/local, but not every sys admin sticks to that path for installing software.
Most Open Source projects provide an option with ''configure'' in the build process to add a prefix and then build their application with this value (./configure --prefix=/usr/local). They install their application in this directory and look for configuration files or other resources there.
Both are common place and acceptable to *nix admins.
Interim
If the environment variable isn''t set or the CLI switch isn''t set, then kick out an error message telling users they must set the path to whatever resource you want to use.
You can try to stick with something like /usr/local, but not every sys admin sticks to that path for installing software.
Most Open Source projects provide an option with ''configure'' in the build process to add a prefix and then build their application with this value (./configure --prefix=/usr/local). They install their application in this directory and look for configuration files or other resources there.
Both are common place and acceptable to *nix admins.
Interim
Also if a symbolic link is used to execute the program, that can also create problems if your result is based on fetching the current working directory to determine the absolute path..
BTW in perl scripts I often do "use FindBin;" to print out where the script was started.. maybe you can take a peek into the modules''s source code to see how you can port this very same code to your program and make it work.
BTW in perl scripts I often do "use FindBin;" to print out where the script was started.. maybe you can take a peek into the modules''s source code to see how you can port this very same code to your program and make it work.
[size="1"]----#!/usr/bin/perlprint length "The answer to life,universe and everything";
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement