views:

48

answers:

2

What is the best practice when running external utilities in shell scripts. For example if I want to run 'gzip' in multiple scripts ensuring that a specific gzip binary is used should I make the full path to the binary a variable and just use the variable thoughout the script?

GZIP=/home/me/bin/gzip
$GZIP somefile
$GZIP anotherfile

Or is it better practice to hard code the path for each call to the binary?

/home/me/bin/gzip somefile
/home/me/bin/gzip anotherfile

Or is it better to set and rely on the path?

PATH=/home/me/bin:$PATH
gzip somefile
gzip anotherfile

I'm not running the scripts as root therefore maintainability, and portability is more important than user security.

+1  A: 

I think the last one is best. If you do not export it, the PATH change will not be visible to programs your script calls, and you probably don't want it visible.

Matthew Flaschen
Even if you *do* export it it will only effect child processes unless the user `source`'s the script rather than running it.
dmckee
+1  A: 

I think the first option is the best. You'll find it or the similar:

SOMEPATH=/path/to/somewhere
EXEC_ONE=exec_foo
EXEC_TWO=exec_bar

$SOMEPATH/$EXEC_ONE --argument baz_file

in many system scripts.

You can use conditionals to test for existence and even compatibilty, available options, etc., and set the variables accordingly.

Dennis Williamson
The really nice thing here is the last point about the conditionals: Depending on what system your script is running on, you may wish to select a different executable to use.
Joe Carnahan