It is currently Fri Nov 28, 2014 9:33 pm

need help figuring out what a snip of code does

All times are UTC - 6 hours

Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sat Jul 12, 2014 12:59 am   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
Pretty n00ber question I am sure, but I can't seem to figure out exactly what this snip of code does, if someone doesn't mind breaking it down for me:
sleep $(( $(date --date="$at" +%s) - $(date +%s) ));

In very general terms the script `sleeps` for the duration in minutes, $at, the user sets before an alarm sounds. I ripped it from another script some time ago that I am now using with great success in an alarm script I hacked together.

The script is quite useful for my purposes and I use it many times a day. But my problem is that whenever I set an alarm that will extend into the next day (past midnight) the alarm simply goes off immediately, and I think it has to be this snip I need to understand better to modify so it allows alarms to extend past midnight.

If it would help to see it in context I could paste the rest of my alarm script.


 PostPosted: Sat Jul 12, 2014 3:33 am   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
Found what I was looking for. I simply need to prepend the word "tomorrow" to the time assigned to var $at. Modified script and alarm does not sound off but is still sleeping, or is still a process at least, so I will assume it is going to work as I need.

But I am still quite hazy about what the snip does exactly or rather how it does what it does. If anyone would like to break it down so it makes more sense I would still appreciate the learning opportunity.

 PostPosted: Mon Jul 14, 2014 8:56 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Perhaps if you could share your alarm script in it's entirety it would be helpful.

 Profile WWW  
 PostPosted: Mon Nov 03, 2014 9:04 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 16
sleep $((     ));     # script sleeps until X seconds past.
                      # duration is caluculated by an math expression: $((  )) 
                      # wiithin the math expression we have two dates:
date -d "$at" +%s    # this takes the hopefully readable date from var "at" and formats it according to
                      # to the format specifier "+%s" actualy giving the number of seconds from Unix epoche ( 1970/1/1 ) until to datetime
                      # specified in "at"
                      # the next "date" statement gives the number of seconds from 1970/1/1 up to now. There is no date set with "-d", hence
                      # "date" assumes now.

                      # so the result is of the "date" commands expansion is something like:
sleep $((  5555 -  5500 )) # giving the seconds from now to date specified in "at"
                      # so the command sleeps the number of secondes from now to date specified in var "at"
NOTE: this got nothing to do with "date"s ability to calculate relative values like "tomorrow" or "next week". The internal representation of a relative date is anyway a floating point number holding date.time. If you use relative time specifications they are translated into absolute date/time values before any calculations are done.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 7 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group