Detect fails in setup script
I wrote a setup script to install my prefered programs and settings after I got a new server.
apt-get install git
git clone http://[email protected] .vim
ln -s .vimrc .vim/vimrc
...
But if something wrong happens during setup, how can I interrupt the setup script, and log the error(s)? For example, if github
server is down, it's obviously useless to create a symbolic link to non-existed vimrc
.
(or you have a better approach to initialize a server?)
Solution 1:
On a basic level, you can do things like redirect STDERR to a text file to log errors
git clone http://[email protected] .vim 2>> ~/logs/vim-error.txt
Or in this particular example you could do an if statement
if [ -f .vimrc ] ## -f means "if file exists"
then
ln -s .vimrc .vim/vimrc
fi
Or after the git clone command you could check its exit status and if it's not 0 exit the script
if [ $? -ne 0 ]
then
exit 1
fi
All of my scripts are n00b level scripts and I've found these things will generally do what I need them to do.
Solution 2:
sure, thats the approach i use. However the principle you are looking for is idempotence and failure tolerance, which is that the script should not mess anything up, and can degrade elegantly. For creating the link, test whether the file exists first like so;
[ -s .vimrc ] && ln -s .vimrc .vim/vimrc
but its also handy to check that the link doesn't exist already; (ie skip if it exists)
[ -s .vimrc ] && [ ! -L .vim/vimrc ] && ln -s .vimrc .vim/vimrc
which basically says, check if the file exists and that it is non-zero, then only create the link if its not there already. There are quite a few file tests supported by bash described in the manual here
There are also usually short cut version of these, but I might as well show you full version first...
typical its handy to run an apt-get update
first, to refresh the package lists...
Obviously there is no point trying to install git if its already installed, (though you might want to upgrade it
dpkg -l git >/dev/null || apt-get -q -y install git
If you just want the latest version, whatever, then don't both with the test and apt will only upgrade if there is one available;
apt-get -q -y install git
I tend to use full paths in installer scripts, just to remind myself and make sure it doesn't do anything silly. eg
#explicitly set the full path somewhere
HOME=/home/tomh
#and then double check, partcular if you are about to do something descructive
[ "$HOME" == "" ] && exit 99
[ -s $HOME/.vimrc ] && ln -s $HOME/.vimrc $HOME/.vim/vimrc
for the getting the file from git I would shoot for something like this;
if [ ! -d $HOME/.vim ]; then
cd $HOME
git clone http://[email protected] .vim
else
cd $HOME/.vim
git pull
fi
check any of that before you use, i didn't test it