I am trying to work out the proper process of installing with Wget, in this example I’ll use Nginx.
# Download nginx to /tmp/ directory
wget http://nginx.org/download/nginx-1.3.6.tar.gz -r -P /tmp
# Extract nginx into /tmp/nginx directory
tar xzf nginx-1.3.6.tar.gz -C /tmp/nginx
# Configure it to be installed in opt
./configure --prefix=/opt/nginx
# Make it
make
# Make install
make install
# Clean up temp folder
rm -r /tmp/*
Is this the idealised process? Is there anything I can improve on?
First of all, you definitely seem to reinvent the wheel: if the problem that you want to solve is automated packaging / building software on target systems, then there are myriads of solutions available, in form of various package management systems, port builders, etc.
As for your shell script, there are a couple of things you should consider fixing:
Stuff like
http://nginx.org/download/nginx-1.3.6.tar.gzornginx-1.3.6.tar.gzare constants. Try to extract all constants in separate variables and use them to make maintaining this script a little bit easier, for example:You generally can’t be 100% sure that wget exists on target deployment system. If you want to maximize portability, you can try to detect popular networking utilities, such as
wget,curl,fetchor evenlynx,links,w3m, etc.Proper practices on using a temporary directory is a long separate question, but, generally, you’ll need to adhere to 3 things:
/tmpis always a temporary directory, as it can be not mounted, it can be non-writable, if can betmpfsfilesystem which is full, etc, etc. Unfortunately, there’s no portable and universal way to detect what temporary directory is. The very least one should do is to check out contents of$TMPDIRto make it possible for a user to point the script to proper temporary dir. Another possibly bright idea is a set of heuristic checks to make sure that it’s possible to write to desired location (checking at least$TMPDIR,$HOME/tmp,/tmp,/var/tmp), there’s decent amount of space available, etc.mktemp --tmpdir -d some-unique-identifier.XXXXXXXXXis usually enough. On BSD-based systems, much more manual work needed, as defaultmktempimplementation is not particularly race-resistant.One should clean up temporary directory after use. Cleaning should be done not only on a successful exit, but also in a case of failure. This can be remedied with using a signal trap and a special cleanup callback, for example:
If you really want to use recursive
rm, then using any*to glob files is a bad practice. If your directory would have more than several thousands of files,*would expand to too much arguments and overflow shell’s command line buffer. I might even say that using any globbing without a good excuse is generally a bad practice. The rm line above should be rewritten at least as:Removing all subdirectories in
/tmp(as in/tmp/*) is a very bad practice on a multi-user system, as you’ll either get permission errors (you won’t be able to remove other users’ files) or you’ll potentially heavily disrupt other people’s work by removing actively used temporary files.Some minor polishing:
taruses normal short UNIX options nowadays, i.e.tar -xvz, nottar xvz.-z,-j,-y, etc. It detects archive/compression format itself andtar -xfis sufficient to extract any of.tar/.tar.gz/.tar.bz2tarballs.