I need to make a Bash script that validates if a date entered by the user is real, among other things. This is what I have done:
while ! $CONDITION # Niega la condición. false es true, true es false.
do
read -p "introduce una fecha. Debe seguir la forma 00/00/0000 - Día/Mes/Año " DATE
if [[ $DATE =~ ^([0-9]{2}[/]){2}[0-9]{4}$ ]] # Se comprueba si la fecha cumple con los criterios
then
DIA=${DATE:0:2}
MES=${DATE:3:2}
ANO=${DATE:6:4}
# Obtenemos las subcadenas del año, entres fracciones.
if []
CONDITION=true
else
echo "Fecha no correcto"
fi
done
I'm not sure how to proceed though. I had thought of using the command date
with the substrings of the date (DAY, MONTH, YEAR) and see what it returns; but no idea. I think there is an output for errors or something, or maybe it's simpler than all that. Can you give me a hand?
Indeed you can use
date
directly passing the date to check with-d "fecha"
. Silence all output and then capture whether or not the command succeeded:Which would be the same as saying:
Notice that it even detects leap years: