Solved

How to prevent some case when input datetime ?

Posted on 2008-06-10
8
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

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 …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

630 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