• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

How to prevent some case when input datetime ?

This code below is check leap year and invalid day completed.
 But I have some problem.I try to make an trap.People insert blank space when enter day, and program not accept and sad input invalid day again ( not blank space).  I try any time it not worked :|. I new to shell scripting. Please help
#!/bin/bash
result="FALSE"
while [ $result = "FALSE" ]
do 
echo -n "Enter day(mm/dd/yyyy): " 
read datetime
if date --date="$datetime" >/dev/null
then
result="TRUE"
else 
result="FALSE"
fi
done

Open in new window

0
megashares
Asked:
megashares
  • 2
  • 2
2 Solutions
 
TintinCommented:
I can't reproduce your problem.  I've tried entering dates with all sorts of spaces and it works fine.

Can you please show us an example.
0
 
megasharesAuthor Commented:
Enter day(mm/dd/yyyy): I press "enter button" or spacebar and enter. I out my program. I want when someone press some thing like this my program will not accept and notice to enter an valid value again
0
 
TintinCommented:
Please note that bash will by default remove whitespace from the end of the input.  However, if you want to check for other whitespace, you can do
#!/bin/bash
while true
do
  echo -n "Enter day(mm/dd/yyyy): "
  read datetime
 
  if echo "$datetime" | grep -q ' '
  then
     echo "Whitespace is not allowed"
     continue
  fi
 
  date --date="$datetime" >/dev/null && break
done

Open in new window

0
 
ahoffmannCommented:
 read datetime
  test -z `echo "$datetime"` && continue
0
 
ahoffmannCommented:
don't see a reason for a delete, valid suggestions are there
could you please explain?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now