fix set -e

This commit is contained in:
Urban Modig
2025-10-01 12:06:27 +02:00
parent 57fcdef4fe
commit 34f6c8dc1d

View File

@ -12,7 +12,7 @@ set -Eeuo pipefail
IFS=$'\n\t' IFS=$'\n\t'
SCRIPT_NAME=${0##*/} SCRIPT_NAME=${0##*/}
VERSION="1.2.1" VERSION="1.2.2"
# ------------------------------- logging & utils ------------------------------- # ------------------------------- logging & utils -------------------------------
log() { printf '[%s] %s log() { printf '[%s] %s
@ -37,40 +37,27 @@ cleanup_tmp() { rm -rf "$WORKDIR" 2>/dev/null || true; }
# ----------------------------- lock/unlock logic ------------------------------ # ----------------------------- lock/unlock logic ------------------------------
# Use a fixed numeric FD (200) for wider Bash compatibility (e.g., macOS bash 3.2). # Use a fixed numeric FD (200) for wider Bash compatibility (e.g., macOS bash 3.2).
LOCK_FD=200
LOCK_FILE="/tmp/${SCRIPT_NAME}.lock" LOCK_FILE="/tmp/${SCRIPT_NAME}.lock"
LOCK_HELD=0 LOCK_HELD=0
lock() { lock() {
log "Creating lock… ($LOCK_FILE)" log "Creating lock… ($LOCK_FILE)"
# Use a literal FD number for portability: exec 200>"$LOCK_FILE" # Open lock file on FD 200 and try to acquire non-blocking lock
exec 200>"$LOCK_FILE" || die "Could not open lock file $LOCK_FILE" exec 200>"$LOCK_FILE" || die "Could not open lock file $LOCK_FILE"
if flock -n 200; then if flock -n 200; then
LOCK_HELD=1 LOCK_HELD=1
else else
die "Lock failed — another process is running." die "Lock failed — another process is running."
fi fi
}>"$LOCK_FILE" || die "Could not open lock file $LOCK_FILE"
if flock -n ${LOCK_FD}; then
LOCK_HELD=1
else
die "Lock failed — another process is running."
fi
} }
unlock() { unlock() {
if (( LOCK_HELD )); then if (( LOCK_HELD )); then
log "Releasing lock…" log "Releasing lock…"
flock -u 200 || true flock -u 200 || true
# Close the FD to avoid lingering descriptors
exec 200>&- || true exec 200>&- || true
LOCK_HELD=0 LOCK_HELD=0
fi fi
} || true
# Close the FD to avoid lingering descriptors
exec ${LOCK_FD}>&- || true
LOCK_HELD=0
fi
} }
# Always cleanup on exit/interrupt # Always cleanup on exit/interrupt