UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old July 11th, 2004, 06:41 PM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
need help on a date script. how to accept proper date ?

i got a date script to get done right quick ...

so i'm supposed to grab the input from the user. user is supposed to enter date. i'm supposed to check whether the input is a date or not.

so the string gets into $1. how can i check if its a date ? i tried to do something like, test blah blah == [0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]

something similar to that. but it wont work. any help to lead me into the right direction i'm a noob when it comes to unix. thanks

Reply With Quote
  #2  
Old July 11th, 2004, 07:06 PM
stevengs stevengs is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Germany
Posts: 394 stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 4 h 36 m 24 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
have you read the manpage for test? I don't think that is what you want to use. Try expr instead.

something like:

expr $1 : '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]'

if the result is 10, meaning 10 matches were found, then it is ok. You could restrict the regex accordingly, if you only want dates from the last and the current centuries.

-Steven
__________________
Am I jumping the gun Baldrick, or are the words "I have a cunning plan." marching with ill deserved confidence in the direction of this conversation? –Edmund Black Adder

Reply With Quote
  #3  
Old July 11th, 2004, 09:28 PM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I finally got it to work. Thanks for your help, Steven!

-Ivalice

Reply With Quote
  #4  
Old July 12th, 2004, 09:34 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
You can also try:
Code:
#!/bin/ksh
# procedure = is_date
# validate date/time a parameter entered
MMDD=$(expr substr "$1" 1 4)
YYYY=$(expr substr "$1" 5 4)
hh=$(expr substr "$1" 10 2)
mm=$(expr substr "$1" 13 2)
ss=$(expr substr "$1" 16 2)
if touch -c -t $YYYY$MMDD$hh$mm.$ss dummyfile
then
   exit 0   # ok
else
   exit 64  # bad
fi

Reply With Quote
  #5  
Old July 12th, 2004, 10:44 AM
stevengs stevengs is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Germany
Posts: 394 stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 4 h 36 m 24 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
wow Jim, that is a really twisted use of touch.

if I understand the logic, you are feeding 'touch' the split string to see if it can update a dummyfile's (that it does not create) access and modification times with it. If it fails, the string was no good, if it succeeds, then the string is ok. Couldn't that be considered "touch abuse"?

At the very least, an excellent excercise.

Do you have some affinity to the number 64?

-Steven
__________________
No. No, not really, the only change is if you could go and put your face in some manure and follow along at a reasonable distance, that would be fine. –Edmund Black Adder

Reply With Quote
  #6  
Old July 12th, 2004, 11:06 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
If you read /usr/include/sysexits.h you'll see exit values.

64 is EX_USAGE, user entry error.

I try to use these values for production scripts. In this example it could just as well have been EX_DATAERR, 65.
Anyway, I know the user messed up either way.

The reason for this is to help me figure out what type of error my script(s) got. exit 1 is not informative at all. IMO. Too many utilities error out with exit 1, so you have no clue where to start looking.

When I get an error value above 63 and less than 78, I know it's trying to tell me something helpful.

If cron scripts exit with EX_NOPERM for example, it's obvious that permissions were wrong.

Reply With Quote
  #7  
Old July 12th, 2004, 11:12 AM
stevengs stevengs is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Germany
Posts: 394 stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 4 h 36 m 24 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
right, that was filed away and forgotten. Thanks again. That is excellent practice and really helps during maintenance.

-Steven

Reply With Quote
  #8  
Old July 13th, 2004, 01:12 AM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
man you guys are too advanced for me ...

this is what i ended up doing below. it seems like a long program compared to jim's!
i'm not familar with that substr command. i will have to look that up. i didn't follow thru your whole program, but if it works, its very efficient.



#############
#!/bin/sh

n=`date -d$1`

if [ $? -ne 0 ]
then
echo "
This is not a valid date format"
echo 'Example of valid formats:

mm/dd/yyyy
mm/dd/yy
month day, year (no spaces in between)'
else

julian=`date +%j`
year=`date +%y`

x=`date +%j -d$1`
y=`date +%y -d$1`


echo "
The julian date of the entered number is: $x"
echo The year of the date enter is: $y


echo "
The current julian date is: $julian"
echo The current year is: $year


echo "
The variable string 'n' is $1"


tup=`expr $y - $year`
tip=`expr $x - $julian`

echo "
The difference in year is $tup"
echo The difference in days is $tip

fi


if [ "$tup" -gt 0 ];
then

z=`expr $tup \* 365`
w=`expr $z + $tip`

echo "
The total days until that date from now is: $w"

else

echo "
The total days until that date from now is: $tip"

fi

Reply With Quote
  #9  
Old July 13th, 2004, 02:29 AM
stevengs stevengs is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Germany
Posts: 394 stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 4 h 36 m 24 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
well, assuming everything else works, there is a small (large) logic error. The number of days will often be incorrect, since you consider every year to be a non-leap year.

-Steven

Reply With Quote
  #10  
Old July 13th, 2004, 01:16 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
This is why I let utility code do the thinking.

There are some very good, fairly complex date scripts in the
FAQ at www.unix.com - see the thread by Perderabo.

Reply With Quote
  #11  
Old July 13th, 2004, 10:49 PM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by stevengs
well, assuming everything else works, there is a small (large) logic error. The number of days will often be incorrect, since you consider every year to be a non-leap year.
-Steven


exactly. but this is an intro to unix class, so the instructor allowed us to neglect that.

thanks for the heads up on that date scripts, jim.

i will be needing everyone's help soon. i have more scripts to write

-i

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > need help on a date script. how to accept proper date ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway