diff --git a/bash/script.md b/bash/script.md new file mode 100644 index 0000000..c99b100 --- /dev/null +++ b/bash/script.md @@ -0,0 +1,21 @@ +## Useful functions + +### Bash trap + +- https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html +```bash +# Define function called before exiting script after an error is caught. +function set_error_status() { + echo "[$(date '+%Y%m%d %H%M%S')] : Something went wrong in the script, exiting." | tee -a "${LOGFILE}" + echo "2 vault-snapshot-restore - KO" > ${STATUSFILE} +} +# Set the function called when the ERR signal is caught. +trap set_error_status ERR +``` + +### Exit immediatly on error and when variables are empty + +- https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html +```bash +set -eu +```