简体   繁体   中英

Bash date/time arithmetic

I have a little Bash script which suspends the computer after a given number of minutes. However, I'd like to extend it to tell me what the time will be when it will be suspended, so I can get a rough idea of how long time I have 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 to the script will be how many minutes from now the computer should be suspended. All I want to add to this script is basically an echo saying eg "Sleeping until HH:nn:ss!". Any ideas?

发现如何。

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

On BSD-derived systems, you'd use

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

ie get the current date in seconds, add the number of minutes, and feed it back to date.
Yeah, it's slightly less elegant.

on linux

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM