Solved

How to prevent some case when input datetime ?

Posted on 2008-06-10
8
177 Views
Last Modified: 2013-12-26
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
Comment
Question by:megashares
  • 2
  • 2
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 21754591
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
 

Author Comment

by:megashares
ID: 21754692
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
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 21754862
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 125 total points
ID: 21755260
 read datetime
  test -z `echo "$datetime"` && continue
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 21816897
don't see a reason for a delete, valid suggestions are there
could you please explain?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question