Bash 日期/时间算术

发布于 2024-07-23 09:37:04 字数 331 浏览 3 评论 0原文

我有一个小 Bash 脚本,它会在给定的分钟数后暂停计算机。 不过,我想延长它,告诉我什么时候会暂停,这样我就可以大概知道我还剩下多长时间。

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

该脚本的唯一参数是从现在开始计算机应该暂停多少分钟。 我想添加到这个脚本中的基本上只是一个 echo ,例如“睡觉直到 HH:nn:ss!”。 有任何想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恰似旧人归 2024-07-30 09:37:05

知道怎么做了。

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

Found out how.

echo "The computer will be suspended at" $(date --date "now $1 minutes")
喵星人汪星人 2024-07-30 09:37:05

在 BSD 派生系统上,您可以使用

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

i.e. 获取当前日期(以秒为单位),添加分钟数,然后将其反馈回日期。
是的,它稍微不那么优雅。

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.

星軌x 2024-07-30 09:37:05

在Linux上

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

on linux

SECS=`date "+$s"`
SECS=$(( SECS + $1 * 60 ))
date --date $SECS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文