Bash date/time arithmetic

自闭症网瘾萝莉.ら 提交于 2020-01-12 18:52:06

问题


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 e.g. "Sleeping until HH:nn:ss!". Any ideas?


回答1:


Found out how.

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



回答2:


On BSD-derived systems, you'd use

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

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




回答3:


on linux

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


来源:https://stackoverflow.com/questions/967780/bash-date-time-arithmetic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!