LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2004, 12:21 PM   #1
clsonnt
LQ Newbie
 
Registered: Jun 2003
Distribution: Redhat
Posts: 14

Rep: Reputation: 0
need to assign serial based on date


I am trying to come up with a way to assign a number indicating the week number based on the date.
e.g.
5 -> 1
12 -> 2

I've tried using a case construct but can't get it to deal with a range of numbers.
What I tried:
Code:
case "$backupdte" in
             1-7      ) serial="1";;
             8-14    ) serial="2";;
             15-21  ) serial="3";;
             22-28  ) serial="4";;
             29-31  ) serial="5";;
esac
What works:
Code:
case "$backupdte" in
             2    ) serial="1";;
             9    ) serial="2";;
             16  ) serial="3";;
             23  ) serial="4";;
             30  ) serial="5";;
esac
Any suggestions?
 
Old 10-28-2004, 12:27 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
How about:
Code:
#!/bin/bash
date +%V
 
Old 10-28-2004, 12:34 PM   #3
clsonnt
LQ Newbie
 
Registered: Jun 2003
Distribution: Redhat
Posts: 14

Original Poster
Rep: Reputation: 0
I guess I need to clarify.....
I need to assign numbers 1 - 5 corresponding to the week number based on the date
e.g.
Oct 1 - Oct 7 -> 1
Oct 8 - Oct 14 -> 2
Oct 15 - Oct 21 -> 3
etc....

Last edited by clsonnt; 10-28-2004 at 12:41 PM.
 
Old 10-28-2004, 01:08 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
So, the value the $backupdte variable is just some day of the month, like 1,2,3,...31 ? Not something like "09312004" or similar?

Doesn't the day of the week make any difference for the week-number? I mean, if $backupdte is "2" should the script always calculate the week number to be "1", no matter if "2" is on a saturday, or a monday?

OK, I assume it's just 1,2,3...31 and the day of the week doesn't matter. Then use:
Code:
#!/bin/bash
serial=$((1+($backupdte-1)/7))
echo $serial
If you want the weeknumber of the current date:
Code:
serial=$((1+($(date +%e)-1)/7))

Last edited by Hko; 10-28-2004 at 01:13 PM.
 
Old 10-28-2004, 01:36 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
By the way, this would work:
Code:
case "$1" in
	[1-7]  ) serial="1";;
But this would not:
Code:
case "$1" in
	[8-14] ) serial="2";;
Because this range trick ( [1-7] ) only works with single characters.

So, when you realy want to use "case", you're stuck with enumerating all of the day-of-the-moth numbers that have more then 1 digit:
Code:
#!/bin/bash

case "$backupdte" in
	[1-7]  ) serial="1";;
	8|9|10|11|12|13|14) serial="2";;
	15|16|17|18|19|20|21) serial="3";;
	22|23|24|25|26|27|28) serial="4";;
	29|30|31) serial="5";;
esac

echo $serial
or do something else:
Code:
if [ $backupdte -le 7 ] ; then serial=1
elif [ $backupdte -le 14 ] ; then serial=2
elif [ $backupdte -le 21 ] ; then serial=3
elif [ $backupdte -le 28 ] ; then serial=4
elif [ $backupdte -le 31 ] ; then serial=5
else serial="ERROR"
fi

echo $serial
 
Old 10-28-2004, 04:26 PM   #6
clsonnt
LQ Newbie
 
Registered: Jun 2003
Distribution: Redhat
Posts: 14

Original Poster
Rep: Reputation: 0
Well......
[1-7] ) serial="1";;
Didn't work. Not sure why but...

8|9|10|11|12|13|14) serial="2";;
Worked like a CHAMP!!! Not quite as clean but I can live with it.

the if ->elseif worked fine too. So I have to different approaches todealing with the problem

THANK YOU
Thank You
thank you!!!

Last edited by clsonnt; 10-28-2004 at 04:29 PM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Filenames based on the date yilez Linux - Newbie 5 11-19-2005 10:11 PM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
Date-based copy AdmiralThrawn Linux - General 2 02-23-2004 09:03 AM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 04:58 PM
can't assign the date in expect wedgeworth Linux - Software 2 09-15-2003 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration