ZFS, flush snapshots older than

Some additional code, which I named zfspurge.sh:

#!/bin/bash
keep=1
num_snaps=$(zfs list -t snapshot -o name -S creation | grep -Po '@autozsys_[a-zA-Z0-9]+$' | uniq | wc -l)
printf "Found %d autosys versions. Configured to keep %d.\n" $num_snaps $keep
if [[ $num_snaps -le $keep ]]; then
    printf "no need to prune.\n"
    exit 0
fi

num_to_prune=$(( num_snaps - keep ))
printf "Pruning %d zsys versions.\n" $num_to_prune
if [[ $num_to_prune -le 0 ]]; then
    printf "Error - no snapshots to prune.\n"
    exit 127
fi

if [[ $num_to_prune -ge $num_snaps ]]; then
    printf "Error - won't remove all snapshots.\n"
    exit 127
fi

for zsys_snap in $(zfs list -t snapshot -o name -S creation | grep -Po '@autozsys_[a-zA-Z0-9]+$' | uniq | tail -n $num_to_prune); do
    printf "Removing $zsys_snap\n"
    sudo zfs list -t snapshot -o name | sudo grep "${zsys_snap}$" | sudo xargs -n 1 zfs destroy -vr
done