-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
69 lines (57 loc) · 1.91 KB
/
utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# shellcheck disable=SC2148
################################################################################
# Implement some useful functions that other languages provide natively.
# See e.g. https://perldoc.perl.org/5.32.0/index-functions.html
#
# This file has no magic number, and is not executable.
# THIS IS INTENTIONAL as it should never be executed, only sourced.
################################################################################
# The default behaviour of `echo` differs between shells, so we deprecate it.
# say (from perl6/raku) forces there to be no escape-char handling at runtime.
# If we want to interpret escape sequences in literals then we should use $''.
# If we want to interpret them at runtime we should explicitly use $(echo -e).
say() {
local text="$1"; shift
if [ "$*" != "" ]; then
die 102 "Too many arguments to say() at line $(caller)"
fi
printf '%s\n' "$text"
}
# warn() is the same as perl
warn() {
local text="$1"; shift
if [ "$*" != "" ]; then
die 102 "Too many arguments to warn() at line $(caller)"
fi
printf '%s\n' "$text" >&2
}
# die() takes two arguments, unlike perl
# do something || die $errnum "$notice"
die() {
local errcode="$1"; shift
printf '%s\n' "$@" >&2
exit "$errcode"
}
# A simple reimplementation of try/catch using one global variable.
# try danger; if catch err && [[ err == 5 ]]; then ...
__posh__try_last_err=0
try() {
__posh__try_last_err=0
eval "$(printf " %q" "$@")" || __posh__try_last_err=$?
}
catch() {
eval "$1"="$__posh__try_last_err"
[ "$__posh__try_last_err" != 0 ]
}
# Find if the first argument is contained in the rest of the arguments.
# This is particularly useful for bash arrays:
# if contains "$element" "${array[@]}"; then ...
contains() {
local i element="$1"; shift
for i; do
if [ "$i" == "$element" ]; then
return 0
fi
done
return 1
}