From c4f09a98dc3c7e1b6b7310e4df1d43bfcb298eb9 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Thu, 24 Aug 2017 23:28:28 +0200 Subject: [PATCH] Add lock script --- lock.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 lock.sh diff --git a/lock.sh b/lock.sh new file mode 100755 index 0000000..e7ee9fc --- /dev/null +++ b/lock.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +LOCKDIR="/tmp/snapshotslocks/" + +[ ! -d "$LOCKDIR" ] && mkdir -p "$LOCKDIR" + +createlock() { + LOCKNAME=$1 + [ -f "$LOCKDIR/$LOCKNAME.lock" ] && exit 1 + touch "$LOCKDIR/$LOCKNAME.lock" + exit 0 +} + +deletelock() { + LOCKNAME=$1 + [ ! -f "$LOCKDIR/$LOCKNAME.lock" ] && exit 1 + rm "$LOCKDIR/$LOCKNAME.lock" + exit 0 +} + +haslock() { + if ls $LOCKDIR/* 1> /dev/null 2>&1; then + exit 0 + else + exit 1 + fi +} + +case $1 in + lock) + createlock $2 + ;; + unlock) + deletelock $2 + ;; + haslock) + haslock + ;; + show) + ls $LOCKDIR + ;; + *) + echo "What is $1" >&2 + exit 1 +esac +