Last active
April 10, 2025 15:33
-
-
Save Kibubu/58e18adaef009faf8163807e8e1398d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Scroll watch: repeat command every INTERVAL and print/log only if output changes. Log or print only if something changes | |
# Default values | |
INTERVAL=2 | |
LOGFILE="swatch.log" | |
QUIET=0 | |
# Parse options | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
-n) | |
INTERVAL="$2" | |
shift 2 | |
;; | |
-o) | |
LOGFILE="$2" | |
shift 2 | |
;; | |
-q|--quiet) | |
QUIET=1 | |
shift | |
;; | |
--) | |
shift | |
break | |
;; | |
-*) | |
echo "Unknown option: $1" >&2 | |
exit 1 | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
# Check if command is provided | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 [-n seconds] [-o logfile] [-q|--quiet] <command>" | |
exit 1 | |
fi | |
PREV_OUTPUT="" | |
# Main loop | |
while true; do | |
RAW_OUTPUT=$("$@") | |
if [[ "$RAW_OUTPUT" != "$PREV_OUTPUT" ]]; then | |
TIMESTAMP="--- $(date) ---" | |
if [ "$QUIET" -eq 1 ]; then | |
{ | |
echo "" | |
echo "$TIMESTAMP" | |
echo "$RAW_OUTPUT" | |
} >> "$LOGFILE" | |
else | |
{ | |
echo "" | |
echo "$TIMESTAMP" | |
echo "$RAW_OUTPUT" | |
} | tee -a "$LOGFILE" | |
fi | |
PREV_OUTPUT="$RAW_OUTPUT" | |
fi | |
sleep "$INTERVAL" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment