Skip to content

Commit 1d7972c

Browse files
committed
Misc fixes all over the place; shellcheck fixes mostly.
checksum_file.sh * explained the OpenBSD situation better * improved print_usage() * moved OS variants into alphabetical order * several shellcheck fixes rsnapshot/rsnapshot-rotate.sh * several shellcheck fixes * set -e * omit WRAPPER_CONF Signed-off-by: Christian Kujau <[email protected]>
1 parent 7ba5642 commit 1d7972c

File tree

4 files changed

+67
-60
lines changed

4 files changed

+67
-60
lines changed

checksum_file.sh

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
# systems, as older shells may not understand command substitution
1414
# with parentheses, as required by POSIX.
1515
# NetBSD: cksum & {g,s}etextattr, should be included in src.
16-
# OpenBSD: No extended attribute support since July 2005 (commit 9dd8235)
16+
# OpenBSD: No extended attribute support since July 2005:
17+
# > https://github.com/openbsd/src/commit/9dd8235"
18+
# > Extended Attributes was a piece to get to ACLs, however ACLs have
19+
# > not been worked on, so EA is pointless. Also the code is not enabled
20+
# > in GENERIC so it is not being tested or maintained.
1721
#
1822
# Each operating system has its own routines for setting/getting EAs and also
1923
# for calculating checksums. The only hardcoded value is the digest algorithm
@@ -26,22 +30,25 @@
2630
# - Or rewrite this whole thing in Python, for portability's sake? (hashlib, os/xattr)
2731
#
2832
DIGEST="md5" # md5, sha1, sha256, sha512
33+
PROG=$(basename "$0")
2934

3035
# Adjust if needed
3136
PATH=/bin:/usr/bin:/sbin:/usr/local/bin:/opt/local/bin:/opt/csw/bin:/usr/sfw/bin
3237

3338
print_usage()
3439
{
35-
echo "Usage: $(basename "$0") [get] [file]"
36-
echo " $(basename "$0") [set] [file]"
37-
echo " $(basename "$0") [get-set] [file]"
38-
echo " $(basename "$0") [check-set] [file]"
39-
echo " $(basename "$0") [check] [file]"
40-
echo " $(basename "$0") [remove] [file]"
41-
echo " $(basename "$0") [test]"
42-
echo ""
43-
echo " get-set - sets a new checksum if none is found, print checksum otherwise."
44-
echo " check-set - sets a new checksum if none is found, verify checksum otherwise."
40+
cat <<EOF
41+
Usage: ${PROG} [get] [file]
42+
${PROG} [set] [file]
43+
${PROG} [get-set] [file]
44+
${PROG} [check-set] [file]
45+
${PROG} [check] [file]
46+
${PROG} [remove] [file]
47+
${PROG} [test]
48+
49+
get-set - sets a new checksum if none is found, print checksum otherwise.
50+
check-set - sets a new checksum if none is found, verify checksum otherwise.
51+
EOF
4552
}
4653

4754
if [ $# -ne 2 ] || [ ! -f "$2" ] && [ ! "$1" = "test" ]; then
@@ -104,24 +111,24 @@ case ${OS} in
104111
${DIGEST} -q "$1"
105112
;;
106113

107-
NetBSD)
108-
# cksum should support all common algorithms.
109-
cksum -q -a ${DIGEST} "$1"
110-
;;
111-
112114
Linux)
113115
# GNU/coreutils should be installed on most Linux distributions.
114116
# It's also by far much faster than its perl or openssl alternatives.
115117
${DIGEST}sum "$1" | awk '{print $1}'
116118
;;
117119

120+
NetBSD)
121+
# cksum should support all common algorithms.
122+
cksum -q -a ${DIGEST} "$1"
123+
;;
124+
118125
SunOS)
119126
# SUNWcsu should be available. If it's not, we'd have much bigger problems.
120127
digest -a ${DIGEST} "$1"
121128
;;
122129

123130
*)
124-
do_log "We don't support ${OS}, yet :-(" 1
131+
do_log "We don't support ${OS} :-(" 1
125132
;;
126133
esac
127134
}
@@ -137,10 +144,6 @@ case ${OS} in
137144
pxattr -n user.checksum.${DIGEST} "$1" 2>/dev/null | awk '/user.checksum/ {print $NF}'
138145
;;
139146

140-
NetBSD)
141-
getextattr -q user checksum.${DIGEST} "$1"
142-
;;
143-
144147
Linux)
145148
# NOTE: If the designated EA is not set, getfattr may not return a non-zero
146149
# exit code. This has been fixed upstream but may not have been picked up
@@ -155,12 +158,16 @@ case ${OS} in
155158
getfattr --only-values --name user.checksum.${DIGEST} -- "$1" 2>/dev/null | awk '/[a-z0-9]/ {print $1}'
156159
;;
157160

161+
NetBSD)
162+
getextattr -q user checksum.${DIGEST} "$1"
163+
;;
164+
158165
SunOS)
159166
runat "$1" cat user.checksum.${DIGEST} 2>/dev/null
160167
;;
161168

162169
*)
163-
do_log "We don't support ${OS}, yet :-(" 1
170+
do_log "We don't support ${OS} :-(" 1
164171
;;
165172
esac
166173
}
@@ -179,20 +186,20 @@ case ${OS} in
179186
pxattr -n user.checksum.${DIGEST} -v "${CHECKSUM_C}" "$1"
180187
;;
181188

182-
NetBSD)
183-
setextattr user checksum.${DIGEST} "${CHECKSUM_C}" "$1"
184-
;;
185-
186189
Linux)
187190
setfattr --name user.checksum.${DIGEST} --value "${CHECKSUM_C}" -- "$1"
188191
;;
189192

193+
NetBSD)
194+
setextattr user checksum.${DIGEST} "${CHECKSUM_C}" "$1"
195+
;;
196+
190197
SunOS)
191198
runat "$1" "echo ${CHECKSUM_C} > user.checksum.${DIGEST}"
192199
;;
193200

194201
*)
195-
do_log "We don't support ${OS}, yet :-(" 1
202+
do_log "We don't support ${OS} :-(" 1
196203
;;
197204
esac
198205
}
@@ -290,7 +297,7 @@ case ${ACTION} in
290297
;;
291298

292299
*)
293-
do_log "We don't support ${OS}, yet :-(" 1
300+
do_log "We don't support ${OS} :-(" 1
294301
;;
295302
esac
296303
;;

rsnapshot/rsnapshot-rotate.sh

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,33 @@
66
CONF="/etc/rsnapshot"
77

88
if [ ! $# = 1 ]; then
9-
HOSTS=$(ls "$CONF"/rsnapshot-*.conf | sed 's/.*\/rsnapshot-//;s/\.conf//' | xargs echo | sed 's/ /|/g')
10-
echo "Usage: $(basename "$0") [$HOSTS]"
9+
HOSTS=$(find ${CONF} -maxdepth 1 -name "*.conf" | sed 's/.*\/rsnapshot-//;s/\.conf//' | xargs echo | sed 's/ /|/g')
10+
echo "Usage: $(basename "$0") [${HOSTS}]"
1111
exit 1
1212
else
13-
CONF="$CONF"/rsnapshot-"$1".conf
14-
DIR=$(awk '/^snapshot_root/ {print $2}' "$CONF")
13+
CONF="${CONF}/rsnapshot-${1}.conf"
14+
DIR="$(awk '/^snapshot_root/ {print $2}' "${CONF}")"
1515

16-
# Don't let rsnapshot-wrapper remount our backup store
17-
WRAPPER_CONF="/usr/local/etc/rsnapshot-wrapper.conf"
18-
NOMOUNT=$(awk -F= '/^NOMOUNT/ {print $2}' $WRAPPER_CONF)
19-
touch "$NOMOUNT"
20-
21-
[ -f "$CONF" ] || exit 2
22-
[ -d "$DIR" ] || exit 3
16+
# Safety belt
17+
[ -f "${CONF}" ] || exit 2
18+
[ -d "${DIR}" ] || exit 3
2319
fi
2420

25-
for i in daily weekly monthly; do
26-
C=$(ls -d "$DIR"/"$i".* 2>/dev/null | wc -l)
21+
set -e
22+
# Don't let rsnapshot-wrapper remount our backup store while we are running.
23+
NOMOUNT=$(awk -F= '/^NOMOUNT/ {print $2}' /usr/local/etc/rsnapshot-wrapper.conf)
24+
touch "${NOMOUNT}"
25+
26+
for interval in daily weekly monthly; do
27+
COUNT=$(find "${DIR}"/${interval}.[0-9] -maxdepth 0 2>/dev/null | wc -l)
2728
j=0
28-
while [ $j -le "$C" ]; do
29-
echo "("$j"/"$C") rsnapshot -c "$CONF" "$i"..."
30-
rsnapshot -c "$CONF" "$i"
29+
while [ "${j}" -le "${COUNT}" ]; do
30+
echo "(${j}/${COUNT}) rsnapshot -c ${CONF} ${interval}..."
31+
rsnapshot -c "${CONF}" "${interval}"
3132
j=$((j+1))
3233
done
3334
done
3435

3536
# Since this is not a normal operation and may take a long time, we will
3637
# remove the NOMOUNT flag again, otherwise we may forget to remove it.
37-
rm -f "$NOMOUNT"
38+
rm -f "${NOMOUNT}"

ternet/flop.sh

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
# https://web.archive.org/web/20170702162404/http://www.breakingsystemsforfunandprofit.com/the-lolcats-they-are-everywhere/
66
#
77
DIR=/var/www/ternet
8-
while read URL; do
8+
9+
while read -r URL; do
910
SURL=$(echo "${URL}" | cut -d" " -f1)
10-
echo "${SURL}" | egrep -qi "\.(jp(e)?g|gif|png|tiff|bmp|ico)$" &&
11-
(
12-
umask 002
13-
PIC=$$-${RANDOM}
14-
wget -q -O ${DIR}/${PIC}.tmp "${SURL}"
15-
convert -quiet ${DIR}/${PIC}.tmp -flop ${DIR}/${PIC}.png
16-
rm -f ${DIR}/${PIC}.tmp
17-
echo http://127.0.0.1/ternet/${PIC}.png
18-
) || echo "$URL"
11+
if echo "${SURL}" | grep -Eqi "\.(jp(e)?g|gif|png|tiff|bmp|ico)$"; then
12+
umask 002
13+
PIC=$$-${RANDOM}
14+
wget -q -O ${DIR}/${PIC}.tmp "${SURL}"
15+
convert -quiet ${DIR}/${PIC}.tmp -flop ${DIR}/${PIC}.png
16+
rm -f ${DIR}/${PIC}.tmp
17+
echo http://127.0.0.1/ternet/${PIC}.png
18+
else
19+
echo "${URL}"
20+
fi
1921
done

vbox-sysrq.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
# Keyboard scancodes: Ordinary scancodes
1717
# https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html#ss1.4
1818
#
19-
# Re: Magic sysrq on Linux guest, under a Linux host
20-
# https://forums.virtualbox.org/viewtopic.php?f=6&t=10400#p207674
21-
#
2219
# Use Magic Sysrq-key in guest
2320
# https://www.halfdog.net/Misc/TipsAndTricks/VirtualBox.html#MagicSysrq
2421
#
@@ -64,8 +61,8 @@ if [ -n "$PRESS" ]; then
6461
VBoxManage controlvm "$VM" keyboardputscancode 1d 38 54 "$PRESS" "$RELEASE" d4 b8 9d
6562
else
6663
echo
67-
echo "Unknown sysrq key! ("${SYSRQ}")"
68-
egrep '^.\|' "$0"
64+
echo "Unknown sysrq key! (${SYSRQ})"
65+
grep -E '^.\|' "$0"
6966
echo
7067
exit 1
7168
fi

0 commit comments

Comments
 (0)