[SOLVED] Advice on best way to proceed with bash script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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
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
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.
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....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.