jueves, 25 de junio de 2009

Error handling in Bash

To check if a command was successfully run in our bash script, we have to check the value of its output, which is stored in the $? variable. If its value equals to 0, then the command was successful, otherwise, an error was thrown:



rm ~/data.tar.bz2
if [ "$?" != 0 ]; then
echo "ERROR: Could not delete file"
exit 1
fi

$ _

1 comentario:

  1. in fact, you can use error code to do conditional execution of commands. For instance:

    rm ~/file.tar && echo "deleted" || echo "not deleted"

    ResponderEliminar