|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
I finally got it to work. Thanks for your help, Steven!
-Ivalice |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
right, that was filed away and forgotten. Thanks again. That is excellent practice and really helps during maintenance.
-Steven |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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 |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
|||
|
|||
|
Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > need help on a date script. how to accept proper date ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|