I found a way to check with if
statement that the input corresponds to a date pattern and is not a eccentric date.
Respectively:
if [[ $dateinput =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]
date -d "$dateinput"
And they work.
I tried to apply it as I can in a while loop as:
dateinput="1992-01-01" #voluntarily made in order to see if the while statement is ok
while [ ! date -d "$dateinput" ]; do
read -p "Indicate a date please: " Date
dateinput=$Date
done
The result is it displays the prompt's text Indicate a date please:
, then displays script.sh: line 73: [[! : command not found
where line 73 is : while [ ! date -d "$dateinput" ]; do
. Besides the variable dateinput
does not exist ever after it because it won't display echo "the input date is: ${dateinput}"
but continues to read and to display the rest of lines of the script.
What can I do to execute it?
bash version: 4.4.20 | OS version: Ubuntu 18.04.3 LTS.