Solved

How to prevent some case when input datetime ?

Posted on 2008-06-10
8
176 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now