67 lines
2.0 KiB
Bash
67 lines
2.0 KiB
Bash
|
#!/bin/sh
|
|||
|
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
|
|||
|
|
|||
|
# Vars [[[
|
|||
|
debug="0"
|
|||
|
|
|||
|
## Colors [[[
|
|||
|
c_redb='\033[1;31m'
|
|||
|
c_magentab='\033[1;35m'
|
|||
|
c_reset='\033[0m'
|
|||
|
## ]]]
|
|||
|
default_mtime_minutes="600"
|
|||
|
|
|||
|
drivedb_list="/tmp/test.css"
|
|||
|
|
|||
|
DDRIVEDB_MSG="green"
|
|||
|
#DDRIVEDB_MSG="red"
|
|||
|
DISK="/dev/sda"
|
|||
|
# ]]]
|
|||
|
## Create or empty a file if it's too old [[[
|
|||
|
regenerate_if_too_old() {
|
|||
|
## Set variables according to the number of passed arguments
|
|||
|
case $# in
|
|||
|
1 )
|
|||
|
_file="${1}"
|
|||
|
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Use default_mtime_minutes value: ${default_mtime_minutes}."
|
|||
|
_max_mtime_minutes="${default_mtime_minutes}"
|
|||
|
;;
|
|||
|
2 )
|
|||
|
_file="${1}"
|
|||
|
_max_mtime_minutes="${2}"
|
|||
|
;;
|
|||
|
esac
|
|||
|
|
|||
|
_current_timestamp=$(date "+%s")
|
|||
|
_file_mtime_timestamp=$(stat --format="%Y" -- "${_file}")
|
|||
|
|
|||
|
## Substract last modification timestamp of the file to current timestamp
|
|||
|
: $(( _file_mtime_seconds=_current_timestamp-_file_mtime_timestamp ))
|
|||
|
## Get maximum allowed mtime in seconds
|
|||
|
: $(( _max_mtime_seconds=_max_mtime_minutes*60 ))
|
|||
|
|
|||
|
## Compare last modification mtime with the maximum allowed
|
|||
|
if [ "${_file_mtime_seconds}" -gt "${_max_mtime_seconds}" ]; then
|
|||
|
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Need to empty or create ${_file} last modification happened ${_file_mtime_seconds} seconds ago (maximum is ${_max_mtime_seconds})."
|
|||
|
true > "${_file}"
|
|||
|
else
|
|||
|
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Don't need to empty ${_file} last modification happened ${_file_mtime_seconds} seconds ago (maximum is ${_max_mtime_seconds})."
|
|||
|
fi
|
|||
|
|
|||
|
}
|
|||
|
## ]]]
|
|||
|
|
|||
|
regenerate_if_too_old /tmp/css_style.css
|
|||
|
regenerate_if_too_old /tmp/font.css 60
|
|||
|
regenerate_if_too_old /tmp/user/1337/serverauth.qGdeK8OOzr 1440
|
|||
|
regenerate_if_too_old /tmp/test.css 600
|
|||
|
|
|||
|
if printf -- '%s' "${DDRIVEDB_MSG}" | grep -q -E -- "green" &&
|
|||
|
! grep -q -- "${DISK}" "${drivedb_list}"
|
|||
|
then
|
|||
|
echo "${DISK}" >> "${drivedb_list}"
|
|||
|
fi
|
|||
|
|
|||
|
|
|||
|
exit 0
|