machine_poweroff.sh: change test of given date.

This commit is contained in:
Christoph 2018-09-21 10:07:03 +02:00
parent f31be4cb63
commit d9eb235789

View File

@ -7,7 +7,7 @@ usage() {
cat<<EOF
Usage: $(basename $0) YYY-MM-DD
Usage: $(basename $0) YYYY-MM-DD
$(basename $0) - Scripts shuts down this machine if the given date is the actual date.
@ -18,29 +18,50 @@ EOF
exit 1
}
[ $# -ne "1" ] && usage "wrong number of arguments"
isValidDate() {
DATE="${1}"
# Autorized separator char ['space', '/', '.', '_', '-']
SEPAR="([ \/._-])?"
# Date format day[01..31], month[01,03,05,07,08,10,12], year[1900..2099]
DATE_1="((([123][0]|[012][1-9])|3[1])${SEPAR}(0[13578]|1[02])${SEPAR}(19|20)[0-9][0-9])"
# Date format day[01..30], month[04,06,09,11], year[1900..2099]
DATE_2="(([123][0]|[012][1-9])${SEPAR}(0[469]|11)${SEPAR}(19|20)[0-9][0-9])"
# Date format day[01..28], month[02], year[1900..2099]
DATE_3="(([12][0]|[01][1-9]|2[1-8])${SEPAR}02${SEPAR}(19|20)[0-9][0-9])"
# Date format day[29], month[02], year[1904..2096]
DATE_4="(29${SEPAR}02${SEPAR}(19|20(0[48]|[2468][048]|[13579][26])))"
# Date 29.02.2000
DATE_5="(29${SEPAR}02${SEPAR}2000)"
# Match the date in the Regex
if [[ "${DATE}" =~ ^(${DATE_1}|${DATE_2}|${DATE_3}|${DATE_4}|${DATE_5})$ ]] ; then
return 0
else
return 1
fi
}
[ $# -ne "1" ] && usage "Wrong number of arguments"
_date=$1
IFS='-' read -a _val_arr <<< "$_date"
declare -i _year="${_val_arr[0]}"
declare -i _month="${_val_arr[1]}"
declare -i _day="${_val_arr[2]}"
__year=${_val_arr[0]}
__month=${_val_arr[1]}
__day=${_val_arr[2]}
if [[ $_month -lt 1 ]] || [[ $_month -gt 12 ]]; then
usage "Wrong date format."
fi
if [[ $_day -lt 1 ]] || [[ $_day -gt 31 ]]; then
usage "Wrong date format."
fi
if [[ $_year -lt $(date +%Y) ]] ; then
usage "Wrong date format."
if ! isValidDate "${__day}-${__month}-${__year}" ; then
usage "Invalid date: ${_date}"
fi
[[ "$(/bin/date +%Y-%m-%d)" == "$_date" ]] && /sbin/poweroff
#[[ "$(/bin/date +%Y-%m-%d)" == "$_date" ]] && echo "ok: $_date " || echo "failed: $_date"
exit 0