My script below the function asks whether you want to install node.js. I know that I can check the success of my last command with $?. But now I have this node() function. How can I efficiently check if an error occured anywhere in my shell function?
node () {
apt-get -y install python g++ make checkinstall
mkdir ~/src && cd $_
wget -N http://nodejs.org/dist/node-latest.tar.gz
tar xzvf node-latest.tar.gz && cd node-v*
./configure
checkinstall -y --install=no --pkgversion 0.10.24 # Replace with current version number.
dpkg -i node_*
cd ~
rm -r ~/src
# If an error occured anywhere in this function,
# an error value should be returned so that the if-clause below fails,
# for to exit the whole script
}
read -p "[q] Install node.js? [y/n] "
if [ $REPLY = "y" ]; then
echo "[x] node script"
node > /dev/null # This should 'get' the error so that ...
else
echo "[s] Skipping installation of node.js"
fi
if [ $? -gt 0 ]; then echo "[e] An error occured"; exit 1; fi # ... it is caught here
echo "[f] Finished successfully"
exit 0
wgetfails (for instance), does it even make sense to continue? If you want to stop on error, you can start your function withset -e....