improve error handling across every script
authorgit <redacted>
Sun, 3 May 2026 19:09:28 +0000 (15:09 -0400)
committergit <redacted>
Sun, 3 May 2026 19:09:28 +0000 (15:09 -0400)
chckip
countconn
esearchlog
searchlog

diff --git a/chckip b/chckip
index c2d8afe2e57136741f0b4bb833e27cfdf68e6eeb..e4d409dbface9265f75f8ea6d6303559d604107a 100755 (executable)
--- a/chckip
+++ b/chckip
@@ -1,5 +1,15 @@
 #!/bin/sh
 
-SERVER=$(which > /dev/null apache2 && printf "apache2" || which > /dev/null nginx && printf "nginx" || printf "ERROR: server not detected, needs to be set manually")
+trap "exit 2" USR1
+
+err() {
+        printf "error: server not detected, needs to be set manually\n" >&2
+        printf "set \$SERVER or modify the script itself\n" >&2
+        kill -USR1 "$$"
+}
+
+# ... && ... || is not an if-then-else, but this works because
+# there's no way for only one printf to fail
+[ -z "$SERVER" ] && SERVER=$(which > /dev/null 2>&1 apache2 && printf "apache2" || which > /dev/null 2>&1 nginx && printf "nginx" || err)
 
 { zcat /var/log/"$SERVER"/access.log.*.gz; cat /var/log/"$SERVER"/access.log /var/log/"$SERVER"/access.log.1; } | grep -a "$@" | awk '{print $1}' | sort | uniq | xargs -L 1 host
index 065227931013e91ed64b7ab0505b427b6b96ed6d..0d71501f61da063885cfd9eeb845a0c24059524d 100755 (executable)
--- a/countconn
+++ b/countconn
@@ -1,6 +1,16 @@
 #!/bin/sh
 
-SERVER=$(which > /dev/null apache2 && printf "apache2" || which > /dev/null nginx && printf "nginx" || printf "ERROR: server not detected, needs to be set manually")
+trap "exit 2" USR1
+
+err() {
+        printf "error: server not detected, needs to be set manually\n" >&2
+        printf "set \$SERVER or modify the script itself\n" >&2
+        kill -USR1 "$$"
+}
+
+# ... && ... || is not an if-then-else, but this works because
+# there's no way for only one printf to fail
+[ -z "$SERVER" ] && SERVER=$(which > /dev/null 2>&1 apache2 && printf "apache2" || which > /dev/null 2>&1 nginx && printf "nginx" || err)
 
 [ "$1" -eq "$1" ] 2>/dev/null && NUM="$1" || NUM=30
 
index 044f1b740fe1c8cd743049c7df9d0380f6f681fc..5d2fe2718fe08c71162c4880c1a322f9813329f3 100755 (executable)
@@ -1,5 +1,15 @@
 #!/bin/sh
 
-SERVER=$(which > /dev/null apache2 && printf "apache2" || which > /dev/null nginx && printf "nginx" || printf "ERROR: server not detected, needs to be set manually")
+trap "exit 2" USR1
+
+err() {
+        printf "error: server not detected, needs to be set manually\n" >&2
+        printf "set \$SERVER or modify the script itself\n" >&2
+        kill -USR1 "$$"
+}
+
+# ... && ... || is not an if-then-else, but this works because
+# there's no way for only one printf to fail
+[ -z "$SERVER" ] && SERVER=$(which > /dev/null 2>&1 apache2 && printf "apache2" || which > /dev/null 2>&1 nginx && printf "nginx" || err)
 
 { zcat /var/log/"$SERVER"/error.log.*.gz; cat /var/log/"$SERVER"/error.log /var/log/"$SERVER"/error.log.1; } | grep --color=auto -a "$@"
index 9d16e63b46e7b98f099b1fd4b763ceea99734dac..a6bd11daf91a5437df5d5b9e4798d5366871fb73 100755 (executable)
--- a/searchlog
+++ b/searchlog
@@ -1,5 +1,15 @@
 #!/bin/sh
 
-SERVER=$(which > /dev/null apache2 && printf "apache2" || which > /dev/null nginx && printf "nginx" || printf "ERROR: server not detected, needs to be set manually")
+trap "exit 2" USR1
+
+err() {
+        printf "error: server not detected, needs to be set manually\n" >&2
+        printf "set \$SERVER or modify the script itself\n" >&2
+        kill -USR1 "$$"
+}
+
+# ... && ... || is not an if-then-else, but this works because
+# there's no way for only one printf to fail
+[ -z "$SERVER" ] && SERVER=$(which > /dev/null 2>&1 apache2 && printf "apache2" || which > /dev/null 2>&1 nginx && printf "nginx" || err)
 
 { zcat /var/log/"$SERVER"/access.log.*.gz; cat /var/log/"$SERVER"/access.log /var/log/"$SERVER"/access.log.1; } | grep --color=auto -a "$@"