Bash Date / Time Arithmetic

I have a little Bash script that pauses the computer after a specified number of minutes. However, I would like to extend it to tell me what time it will be when it will be suspended, so I can get a general idea of ​​how long I left, so to speak.

#!/bin/sh let SECS=$1*60 echo "Sleeping for" $1 "minutes, which is" $SECS "seconds." sleep $SECS && pm-suspend 

The only argument for the script will be the time after which the computer should be suspended after a few minutes. All I want to add to this script is basically echo , for example, for example. "Sleeping up to HH: nn: ss!". Any ideas?

+7
date bash time sleep
source share
3 answers

Discovered how.

 echo "The computer will be suspended at" $(date --date "now $1 minutes") 
+9
source share

In BSD-derived systems you should use

 date -r $(( $(date "+%s") + $1 * 60 )) 

i.e. get the current date in seconds, add the number of minutes and return it to the date.
Yes, it's a little less elegant.

+1
source share

in linux

 SECS=`date "+$s"` SECS=$(( SECS + $1 * 60 )) date --date $SECS 
+1
source share

All Articles