68 lines
1.6 KiB
Bash
Executable File
68 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
usage() {
|
|
if [ -n "$1" ];then
|
|
echo -e "\n [ Error ]: $1"
|
|
fi
|
|
|
|
cat<<EOF
|
|
|
|
Usage: $(basename $0) YYYY-MM-DD
|
|
|
|
$(basename $0) - Scripts shuts down this machine if the given date is the actual date.
|
|
|
|
The most common practice is, to use this script as cronjob.
|
|
|
|
EOF
|
|
|
|
exit 1
|
|
}
|
|
|
|
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"
|
|
|
|
__year=${_val_arr[0]}
|
|
__month=${_val_arr[1]}
|
|
__day=${_val_arr[2]}
|
|
|
|
if ! isValidDate "${__day}-${__month}-${__year}" ; then
|
|
usage "Invalid date: ${_date}"
|
|
fi
|
|
|
|
[[ "$(/bin/date +%Y-%m-%d)" == "$_date" ]] && /sbin/poweroff
|
|
|
|
exit 0
|