bin/warn-no-recent-login.sh

82 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s globstar nullglob
function show-help ()
{
local -ir retcode="${1:-1}"
cat >&2 <<EOF
usage: $(basename "$0") [OPTION...] DAYS
Warn if the invoking user has not logged in within the past DAYS days.
OPTIONS
-v Verbosity. Repeat for more verbosity.
-h This help.
Examples
\$ $(basename "$0") 20
\$ $(basename "$0") -vv 30
EOF
exit "$retcode"
}
#----------------------------------------------------------------------------
declare -i VERBOSITY=0
while getopts ":hv" opt
do
case $opt in
v ) ((++VERBOSITY)) ;;
* ) show-help 0;;
esac
done
shift $((OPTIND - 1))
#----------------------------------------------------------------------------
function squawk ()
{
local -i NVOL=$1; shift
if ((0 == NVOL)); then
echo >&2 "$(basename "$0") $(date +'%T %Z')" "$@"
exit 1
elif ((NVOL <= VERBOSITY)); then
echo "$(basename "$0") $(date +'%T %Z')" "$@"
fi
return 0
}
#----------------------------------------------------------------------------
# Express the VERBOSITY as an option string.
function vopt ()
{
local str=""
if ((0 < VERBOSITY)); then
str="-v"
fi
for ((i = 1; i < VERBOSITY; i++)); do
str="${str}v"
done
echo "$str"
return 0
}
#----------------------------------------------------------------------------
((1 == $#)) || show-help 99
if [[ -z $1 || $1 = *[!0123456789]* || (($1 < 1)) ]]; then
squawk 0 "DAYS must be a positive integer."
fi
declare -ir days_ago="$1"
squawk 1 "$(date --iso) started."
if ! has-logged-in-since.sh $(vopt) "$days_ago"
then
squawk 0 "no login by $LOGNAME in the past $days_ago days."
fi
squawk 1 "done."
exit 0