Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active July 30, 2020 21:12
Show Gist options
  • Save JFFail/093100bdeead1850b5c9895962a5c882 to your computer and use it in GitHub Desktop.
Save JFFail/093100bdeead1850b5c9895962a5c882 to your computer and use it in GitHub Desktop.
Simple Bash script to calculate the number of days in quarantine.
#!/bin/bash
# Print help if it was requested.
if [[ $1 == "-h" || $1 == "-?" ]]; then
printf "quarantine_days - Provide a date in the format of YYYY/MM/DD\n"
printf "\tIf the date is valid, the number of days in quarantine will be printed.\n"
printf "\tNon-valid dates will cause the script to exit.\n"
exit
fi
# Make sure something was passed.
if [[ ! $1 =~ ^[0-9]{4}(\/)(((0)[0-9])|((1)[0-2]))(\/)([0-2][0-9]|(3)[0-1])$ ]]; then
printf "You did not enter a valid date."
exit
fi
# Make the dates.
today=$(date +%s)
starting=$(date -d $1 +%s 2> /dev/null)
# Make sure the date was valid. Regex doesn't cover everything.
if [[ $starting == "" ]]; then
printf "%s is not a valid date.\n" $1
exit
fi
# Subtract the dates and convert to days.
diff_days=$((($today - $starting) / 86400))
# Print it.
printf "%s\n" $diff_days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment