Register
It is currently Tue Sep 30, 2014 10:00 pm

Can someone give me a hand?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Feb 28, 2011 12:54 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
I'm trying to make a script that makes sure the date the user enters is a valid date (i.e. 20010229 is not a valid date as there is no 29th of february in 2001).
If the user inputs an invalid date, an error is generated... so what i want is to store the error in the variable IF it occurs, but i dont want the error to be displayed in the script unless i'm calling the variable

i.e. bday=20010229

date -d "$bday"
outputs the error
date: invalid date `20010229'

Can someone help me out? :)


Top
 Profile  
 PostPosted: Mon Feb 28, 2011 2:27 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Welcome to the forum broken86!

There are several ways to do this, depending on what you want to do with the end result, and if you want to reformat it in any way. Here's a fairly simple one:

Code:
#!/bin/sh
bday=$1

# if this 'date' command doesn't return "true", we
# set $bday to "invalid..."
if ! date -d "$bday" > /dev/null 2>&1; then
    bday="invalid date $bday"
fi

echo $bday


Here's an example of a good result and a bad one:
Code:
~$ sh tmp.sh 20010228
20010228

~$ sh tmp.sh 20010229
invalid date 20010229


I hope this helps!
-Jeo


Top
 Profile YIM  
 PostPosted: Mon Feb 28, 2011 3:06 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
Hm wow that's working a lot better than what I had been trying earlier, only problem is it's still outputting the error =\ Here's what I'm doing
Code:
elif ! date -d "$bday" > /dev/null 2>&1; then
        echo "You entered $bday, which is an invalid date, please enter your birthdate in the form YYYYMMDD."


and its returning:

Enter your birth date in the format yyyymmdd (i.e. 19861024)

20010229
date: invalid date `20010229'
You entered 20010229, which is an invalid date, please enter your birthdate in the form YYYYMMDD.

Am i missing something?


Top
 Profile  
 PostPosted: Mon Feb 28, 2011 3:15 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
What's the rest of that script look like? Is the 'date' command called again somewhere?

The " > /dev/null 2>&1 " should redirect actual the output of our 'date' command to /dev/null (so we shouldn't be seeing it...).


Top
 Profile YIM  
 PostPosted: Mon Feb 28, 2011 3:44 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
i'm only calling it in variables using command substitution like
date=`date +%Y%m%d`

do i need to redirect all date commands to dev null?


Top
 Profile  
 PostPosted: Tue Mar 01, 2011 3:03 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
broken86 wrote:
i'm only calling it in variables using command substitution like
date=`date +%Y%m%d`

do i need to redirect all date commands to dev null?


You are capturing the output of date but not the error output, assuming you don't want to capture or display the error;

Code:
date=$(date +%Y%m%d 2>/dev/null)


Top
 Profile  
 PostPosted: Tue Mar 01, 2011 4:37 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
that seems to be working thanks for your help! :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP