Register
It is currently Sun Nov 23, 2014 11:14 pm

which day is that date?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu May 10, 2012 10:34 am   

Joined: Thu May 10, 2012 10:23 am
Posts: 2
So i have a littel problem..i know its basic..but im a newbee..

i want to make a script, wich sais which day is that date what i write for it..

so example i write 20120510 (yyyymmdd)
and it write thursday..
and if date is not valid it have to write that "its not a correct date."

i tried the date
Code:
#!/bin/bash
read datee
date --date $datee +%a
its write the correct day..but how decide the program that the date is not valid if a write ex 20011535?

thx in advance


Top
 Profile  
 PostPosted: Thu May 10, 2012 4:10 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
The 'date' command can deal with all kinds of input, and will tell you if the date you've given it is invalid. You should get output like: invalid date `20011535'

Is your question about validating the input BEFORE it's passed to the 'date' command, or do you just want to intercept the output of 'date' and print your own message if the date is wrong?


Top
 Profile YIM  
 PostPosted: Thu May 10, 2012 10:33 pm   

Joined: Thu May 10, 2012 10:23 am
Posts: 2
yes i want to check the validity of the date before 'date' command


Top
 Profile  
 PostPosted: Sun May 13, 2012 10:06 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
It sounds like you want to make valid only one date(1)'s --date input string. What's the point of doing this,
since the said command's output will always be identical for different input strings meaning the same thing?

Consider this as a feature and use date(1) to check whether the given date was valid, like so:

Code:
#!/bin/bash

read -rp 'Date input string: '

if ! date -d "$REPLY" +%a 2> /dev/null; then
    printf '\nerror: « %s » is not a valid date.\n' "$REPLY" >&2
    exit 1
fi


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 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:  
cron


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