From ce39cc3736c0bc65ce589639225301b79add4cf8 Mon Sep 17 00:00:00 2001 From: kirby Date: Wed, 4 Jun 2025 09:40:29 +0200 Subject: [PATCH] add bash section --- bash/script.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 bash/script.md 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 +```