*: Move die() into .local/bin/helpers.sh

The function `die` was redundantly implemented in various files.

Move the function into .local/bin/helpers.sh and source that where
previously implemented.

Also prepend the program's name to the message and always terminate the
message with a newline. The newline was previously needed for a small
but unnecessary hack that prevented the need of the `[ -z "$1" ]` test.
This commit is contained in:
2022-09-22 14:29:35 +02:00
parent d7c9bda8a7
commit 7666086dba
5 changed files with 31 additions and 27 deletions

View File

@@ -8,14 +8,12 @@
## format of zshs extended history.
## An automatic backup is created before deleting that can be used for recovery.
die() {
printf "$1" >&2
exit ${2:-1}
}
# Source die()
. "$HOME"/.local/bin/helpers.sh
[[ $# -eq 1 ]] || die "Specify a history file.\n"
[[ -e "$1" ]] || die "File does not exist.\n"
[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off.\n"
[[ $# -eq 1 ]] || die "Specify a history file."
[[ -e "$1" ]] || die "File does not exist."
[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off."
# Take a history file per stdin and sort the commands per number of occurrences
hist-sort() {

13
.local/bin/helpers.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/sh
#
# A collection of useful shell functions. This file should be sourced, rather
# than executed.
# Print error message, prepended by the programs name, and then exit
#
# Usage: die [<MESSAGE>] [<EXIT_CODE>]
die() {
[ -z "$1" ] || >&2 printf "%s: %s\n" "$0" "$1"
exit ${2:-1}
}