LinuxQuestions.org
Help answer threads with 0 replies.
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 02-23-2015, 06:26 PM   #1
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Rep: Reputation: 46
Advice on best way to proceed with bash script


Ladies & Gents,

First thanks again for all the help provided by everyone here.

I am ready to implement the next feature of the bash script that I have been working on forever. The thing is I am not sure how best to go about it.

What needs to happen:
1. Determine if the day in the que requires 'candle lighting' time to be set. That takes place by default 20 min before sundown, meaning that if the day in que is Thursday candle lighting time is Wednesday evening. The issue is that hdate, which returns candle lighting time for the weekly Shabbat (Saturdays) does not do so for the Festivals, unless they fall on a Shabbat, so it will have to be done a different way. I thought to actually generate a csv file with those days to test against.

2. There is also the issue of whether the ScriptUser is using 'diaspora' Festivals or not as that adds days that require candle lighting times. That raises the level of complexity somewhat. I thought to store that info in a different csv file that already exists because it is needed to get the correct readings for each day. Eventually I plan on making a test to see if that file exist and if not ask the user a series of questions to generate it, but that is a later thing.

3. Then there is the issue of if the 'day in que' is actually a Friday, in which case candle lighting time can be easily obtained via hdate.

4. Looking at 'date' I have not seen any way to actually and easily add time to the output. Other than using the epoch time as candle lighting time changes by about 5 min per week where I live so at times it has to step over the hour to the next one.

I am sure there are other issues that I have not thought of yet that will come up.

Any suggestion as to the most practical way to implement the above? Or thoughts to share? Or questions?

Thanks
 
Old 02-23-2015, 09:04 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
date is pretty powerful in this respect:
Code:
$ date -d 'now -1 day + 59 minute'
Mon Feb 23 12:02:14 AWST 2015
You should be able to find a way to work with that based on your previous experience
 
Old 02-25-2015, 07:08 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
To add to grail's suggestion, you can also change the output format of the date using [+FORMAT] where it might appear as something like this:
Code:
$ date +%m%d%H%M
02250805
Thus removing inconvenient spacings or punctuations, or adding them if that also helps your script.

Look at the man page for date(1) and see the section describing the FORMAT to control the output the way you'd like and just experiment with the command line to assist your choice for final format.
 
Old 02-27-2015, 01:47 PM   #4
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks for your input guys.

It seams that date does not like to have time input when trying to add or subtract days with -d. It causes it to do some strange things.

Code:
:~$ date -d "2015/02/27 18:24 - 1 day" "+%d %m %Y %H:%M"
28 02 2015 14:24  

# let's just add a day instead of subtracting it.  OH and by the
#  way we will give you the wrong time too just because you asked.

:~$ date -d "2015/02/27 - 1 day" "+%d %m %Y %H:%M"
26 02 2015 00:00

:~$ date -d "2015/02/27 18:24 - 20 minute" "+%d %m %Y %H:%M"
28 02 2015 09:25
:~$
So it looks like I may have to make the time adjustment the hard way, with real math. I have to take say 14:24 and subtract 20 minutes from it and it is a 24 hr clock

yea ha
 
Old 02-27-2015, 02:08 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by rbees View Post
It seams that date does not like to have time input when trying to add or subtract days with -d. It causes it to do some strange things.
Date math in bash, what fun.

Found this clip in my stash:
Code:
echo $((($(date +%s)-$(date +%s -d "May 21, 1992"))/86400))
May help, or just be a "wtf is he talking about now?"
 
Old 02-27-2015, 02:28 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Seems like it joins the "- 1" to the time. Adding a timezone suffix works:
Code:
$ date -d "2014/02/27 18:24EST -1 day" "+%d %m %Y %H:%M"
26 02 2014 18:24
Replace EST with your actual timezone, or else set everything to UTC:
Code:
$ TZ=UTC0 date -d "2014/02/27 18:24Z -1 day" "+%d %m %Y %H:%M"
26 02 2014 18:24
Using "x days ago" instead of "-x days" also works:
Code:
$ date -d "2014/02/27 18:24 1 day ago" "+%d %m %Y %H:%M"
26 02 2014 18:24
Reference:
Combined-date-and-time-of-day-items.
Relative-items-in-date-strings
 
Old 02-27-2015, 02:34 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Don't start too complicated, start with:
Code:
$ date -d "-1 day"
Thu Feb 26 15:31:42 EST 2015
And THEN change the format:
Code:
$ date -d "-1 day" +%m%d%H%M
02261533
 
Old 02-27-2015, 03:22 PM   #8
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks guys,

Adding the time zone did it.

So here is what it is suppose to do, broken down and simplified.
Quote:
1. Is the current Yom Tove (passed in by $YOMTOVE) in the "CandleData.csv"
a. IF day is Friday; then return
b. ELIF day is Sunday; then grab sundown from the day before (Saturday) as candle lighting time; then return
c. ELSE grab sundown from the day before and calculate candle lighting time at 20 min before sundown; FI
And this is the code I have come up with.
Code:
# Yom Tove Candle Lighting Time
# Needs DAY MONTH YEAR LAT LONG TZ and YOMTOVE passed in
function YTCandle_Time {
  if [[ `grep -e "$YOMTOVE" "$STORDIR"/CandleData.cvs` == "" ]];then
      echo "This Yom Tove does not need candles lit"
      return
  else
      echo "This Yom Tove need candle lighting times setup"
      if [[ `date -d $YEAR-$MONTH-$DAY +%a` == "Sat" ]];then
	return # as candle lighting time is set in another part of the script
      elif [[ `date -d $YEAR-$MONTH-$DAY +%a` == "Sun" ]];then
	YTCT="$(hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/sunset/{print $2}')"
	read DAY MONTH YEAR < <(date -d "$YEAR-$MONTH-$DAY - 1 day" "+%d %m %Y")
	at "$YTCT" "$MONTH/$DAY/$YEAR" -f "$STORDIR"/shofar
	return  # candle lighting time should be set to sundown
      else YTCT="$(hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/sunset/{print $2}')"
      read DAY MONTH YEAR HOUR MIN < <(date -d "$YEAR-$MONTH-$DAY $YTCT$TZ -1days20minutes ago" "+%d %m %Y %H %M")
      	at "$HOUR:$MIN" "$MONTH/$DAY/$YEAR" -f "$STORDIR"/shofar
      fi
  fi
}
So does it look right?

Last edited by rbees; 02-27-2015 at 03:46 PM. Reason: code correction
 
Old 02-27-2015, 04:17 PM   #9
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Well it mostly works. But I am having an issue with this line I think.

Code:
`grep -e "$YOMTOVE" "$STORDIR"/CandleData.cvs` == ""
I think it is being to broad of in searching for the csv, and I do see that the files are not named correctly. What I mean is that $YOMTOVE will contain say "Pesach I" but the csv will have both Pesach I and Pesach II, so it will return yes when it shouldn't. On top of that I lost somewhere my little grep hand book :grrrrr But my time to play is about up as Shabbat is almost here.

Thanks again.
 
Old 02-27-2015, 04:39 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Code:
  if [[ `grep -e "$YOMTOVE" "$STORDIR"/CandleData.cvs` == "" ]];then
This is better rendered as

Code:
  if ! grep -Fqe "$YOMTOVE" "$STORDIR"/CandleData.cvs; then
For the "Pesach I" vs "Pesach II" issue, since you're searching comma separated data just add a comma:

Code:
  if ! grep -Fqe "$YOMTOVE", "$STORDIR"/CandleData.cvs; then
 
Old 03-01-2015, 11:58 AM   #11
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks ntubski,

It turned out that neither of the two above proved to be the problem. Some body forgot to reset the date variables $DAY $MONTH & $YEAR back to the current day after grabbing sundown from the previous day to set candle lighting time in the last action.

As for the extra porcessing that I thought was being caused by the simalar names, I had the csv data file open in Libre Office to get the proper number of fields in the correct place and when I save it Libre Office Calc promptly add commas to the end of each line so all the lines had the same number of fields. So a different part of the script was processing the blank fields and generating empty 'at' jobs for them.

So now I get to start adding complexity to account for things like if the Yom Tove is an intermediate day > which day of the week is it > then do some other thing > but only if some different thing is true....... I don't understand it yet, so....

Thanks again
 
  


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
How to make shell script wait for a particular key pressed in order to proceed? Caed Lucin Linux - General 5 01-31-2010 06:08 PM
How to compare a text value in a shell script and proceed accordingly hemantsankhla Linux - Newbie 8 09-18-2009 12:42 AM
How to make shell script wait for key press to proceed... ddenton Linux - General 13 12-02-2008 04:25 AM
Seeking advice on bash script satimis Programming 6 10-11-2004 11:01 AM
Need some advice on how to proceed with -> proendo Slackware 1 01-27-2004 10:46 AM

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

All times are GMT -5. The time now is 10:06 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