views:

100

answers:

3

I have a Bash script that basically initializes an application and sets parameters. One of these parameters is the location to OpenOffice. Now OpenOffice doesn't set an environment variable when you install it.

What is the best method of finding the location of an application installed and caching that information so you don't have to do the I/O next time?

What I was thinking was simply running a find on /usr/ for the OpenOffice directory which has a specific file. When it's found store that directory in a environment variable and in this script check if the environment variable is set and is a directory, if so use it, if not search again.

This would allow the script to work without user interaction but also allow the user to set a path themselves (since it's an environment variable).

This seems like a "bad practice", so I'm hoping maybe someone else can give me the common way of getting information about a software install. If it helps, OpenOffice will most likely be installed using aptitude.

+1  A: 

The way I've usually seen it done is with some code like this:

[ -z "$OO_EXE" ] && OO_EXE=/usr/bin/oo # or whatever the executable is called

Then you can always assume that the OO_EXE environment variable is set later on in the script. The user can override the default by setting OO_EXE in the environment before running the script, but if he doesn't then the script falls back to the default.

If you want to dynamically find the default the first time the script is run and reuse it every other time, then I would prompt the user for the path the first time the script is run, drop a dot file in the user's home directory, and read from that file every other time.

Zach Hirsch
Smart idea. This script is a /etc/init.d script to handle running a daemon. Is it ever a common practice to have to ask that kind of question when running for the first time? Maybe I should be asking them this information when they install the daemon?
William
Yea, I think asking when they install the daemon is a good idea. Prompting from an init.d script isn't too good. Since it's a daemon, though, it would probably be better to drop the config file in /etc rather than a user's home directory.
Zach Hirsch
Actually yet another question. When you install an application using a packaging system (like aptitude or yum) and a package has a dependency on another package. How does it know where the file is located if the package says it's there already? It seems to me that packages are installed in different directories based on the distro, this could be wrong. Do they just check the default, if it's not there ask the user for input? You might not know this answer but if you do it would be helpful. :)
William
There is a central config in the etc directory, but it's more for the actual application and not the shell script.
William
Most package managers have a database that they maintain of installed programs. If you install something without going through apt, then apt won't know that you've installed it. If you do install it through apt, though, it'll know that it's already installed.
Zach Hirsch
Yeah, but just because it knows it's installed, doesn't mean it knows where it is. Does the packaging system ALWAYS install it in the same directory on EVER distro? I'm creating a debian package that I could check to see if the openoffice.org package is installed, and if so "assume" it's at a certain directory. If it's not, then ask. But is that the "common" way? Or because the package is installed, it doesn't even check?
William
The package hard codes where it will be installed. For example, the openoffice package is probably hard coded to install everything under /usr/bin, /usr/lib, etc. The same package will install the files into the same place on every distribution (assuming that you can install it on that distribution).
Zach Hirsch
+1  A: 

Many applications set such values when they install themselves. For example, when a user runs the configure script for your app, the user has the ability to specify the location of ooffice. The script will use that value or try to find it or use a default if the user leaves it unspecified. When the app is installed, it will have a hard-coded value.

William Pursell
+1 for your answer. It looks like I will end up doing this. Thanks for the help guys!
William
A: 

You could use the command which.

asafe@mimimi:~$ which openoffice.org 
/usr/bin/openoffice.org
asafe@mimimi:~$ 
Hai