aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/shells/zsh.md
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2024-08-20 01:03:48 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2024-08-20 01:04:22 +0200
commit88fc5bc98370066490a9670c6b28fba91efd7d4d (patch)
tree630ff0e49badd3f0c660b933a142c860beb1f679 /src/shells/zsh.md
parent7ca41565c4e70b212cb88a4d18c18fa35768df6d (diff)
downloadnotes-88fc5bc98370066490a9670c6b28fba91efd7d4d.tar.gz
notes-88fc5bc98370066490a9670c6b28fba91efd7d4d.zip
zsh,bash: trap handling
Diffstat (limited to 'src/shells/zsh.md')
-rw-r--r--src/shells/zsh.md35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/shells/zsh.md b/src/shells/zsh.md
index f702655..6be1967 100644
--- a/src/shells/zsh.md
+++ b/src/shells/zsh.md
@@ -238,6 +238,40 @@ if [[ $INPUT =~ $REGEX ]]; then
fi
```
+## Trap Handling
+
+```zsh
+trap "<CMDs>" <SIG>/EXIT
+
+# Show current trap handler.
+trap -p
+# List signal names.
+trap -l
+```
+
+#### Example: Run handler only on error exit
+```zsh
+trap 'test $? -ne 0 && echo "run exit trap"' EXIT
+
+# -> no print
+exit 0
+# -> print
+exit 1
+```
+
+#### Example: Mutex in shell script
+For example if a script is triggered in unsynchronized, we may want to ensure
+that a single script instance runs.
+```zsh
+# open file=LOCK with fd=100
+exec 100>LOCK
+# take exclusive lock, wait maximal for 3600s
+flock -w 3600 -x 100 || { echo "flock timeout"; exit 1; }
+
+# eg automatically release lock when script exits
+trap "flock -u 100" EXIT
+```
+
## Completion
### Installation
@@ -366,6 +400,5 @@ Explanation:
> - [zsh completion functions][zsh-comp-fn]
> - [zsh completion utility functions][zsh-comp-utility-fn]
-
[zsh-comp-fn]: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
[zsh-comp-utility-fn]: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#utility-functions