curlgrep-notify/curlgrep.sh
2023-08-14 09:58:51 +02:00

56 lines
1 KiB
Bash
Executable file

#!/bin/bash
STATE_DIR="$(dirname $(readlink -f "$0"))/_state"
URLS=(
'https://tmp.tobast.fr/testcurl'
)
PATTERNS=(
'1'
)
MESSAGES=(
'match'
)
function do_one {
if [ "$#" -lt 4 ] ; then
>&2 echo "Missing argument to do_one."
return
fi
url="$1"
pattern="$2"
msg="$3"
elt_id="$4"
state_path="$STATE_DIR/$elt_id"
curl -s "$url" | grep -q "$pattern"
no_match="$?"
if [ "$no_match" -eq 1 ]; then
if [ -f "$state_path" ] ; then
rm -f "$state_path"
fi
else # match
if ! [ -f "$state_path" ] ; then # no previous match
echo "NEW MATCH: $msg"
touch "$state_path"
fi
fi
}
if [ "${#URLS[@]}" -ne "${#PATTERNS[@]}" ] \
|| [ "${#URLS[@]}" -ne "${#MESSAGES[@]}" ]
then
>&2 echo "Bad configuration: unbalanced number of elements"
exit 2
fi
mkdir -p "$STATE_DIR"
for elt_id in "${!URLS[@]}"; do
do_one \
"${URLS[$elt_id]}" \
"${PATTERNS[$elt_id]}" \
"${MESSAGES[$elt_id]}" \
"$elt_id"
done