Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

You could use the command which.

asafe@mimimi:~$ which openoffice.org 
/usr/bin/openoffice.org
asafe@mimimi:~$ 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top