Solved

How to prevent some case when input datetime ?

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
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…

752 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