AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1257479
Accepted
CJ--
CJ--
Asked: 2020-07-09 12:53:37 +0800 CST2020-07-09 12:53:37 +0800 CST 2020-07-09 12:53:37 +0800 CST

在bash中减去日期格式

  • 772

我正在尝试创建倒计时 - 很可能是艰难的方式。我有这样的设置:

#! /bin/bash 

#When to execute command 
execute=$(date +"%T" -d "22:30:00")

#Current time
time=$(date +"%T")

#Subtract one from the other.... the below is line 11
math=$(("$execute"-"$time"))

#Repeat until time is 22:30:00
until [ "$math" == "00:00:00" ]; do  

echo "Countdown is stopping in $math"
sleep 1
clear 
done

问题是......它不起作用。这是终端中的输出:

   /path/to/file/test.sh: line 11: 22:30:00-16:39:22: syntax error in expression (error token is “:30:00-16:39:22”) 
    Countdown is stopping in 

首先,错误信息,有什么问题?

其次,“倒计时正在停止”消息应该有倒计时将停止的小时、分钟和秒。为什么不呢?请记住,我不是专业人士。

scripts bash time date syntax
  • 2 2 个回答
  • 1262 Views

2 个回答

  • Voted
  1. Best Answer
    P.P
    2020-07-09T13:32:35+08:002020-07-09T13:32:35+08:00

    问题在于声明

    math=$(("$execute"-"$time"))
    

    因为executeandtime包含格式中的值%H:%M:%S。但是 bash 的算术扩展不能评估时间格式。

    除了%H:%M:%S格式,您可以将时间转换为秒,进行算术运算,然后以所需的格式打印。

    就像是

    #!/bin/bash
    
    #When to execute command
    execute=$(date +"%s" -d "22:30:00")
    
    time=$(date +"%s")
    math=$((execute-time))
    
    if [[ $math -le 0 ]]; then
        echo "Target time is past already."
        exit 0
    fi
    
    #Repeat until time is 22:30:00
    while [[ $math -gt 0 ]]; do
        printf "Countdown is stopping in %02d:%02d:%02d" $((math/3600)) $(((math/60)%60)) $((math%60))
        sleep 1
        clear
    
        # Reset count down using current time;
        # An alternative is to decrease 'math' value but that
        # may be less precise as it doesn't take the loop execution time into account
        time=$(date +"%s")
        math=$((execute-time))
    done
    
    • 1
  2. CJ--
    2020-07-09T16:11:15+08:002020-07-09T16:11:15+08:00

    谢谢@PP 的回答。您的方法起初有效,但在重新启动后停止工作....此外,它并没有停止循环 - 这意味着它进入负数并且之后从未执行过命令。这就是我最终做的事情:

    #! /bin/bash
    
    #When to execute command
    execute=$(date +"%s" -d "22:30:00")
    
    time=$(date +"%s")
    math=$((execute-time))
    
    #Repeat until time is 22:30:00
    until [ "$time" == "$execute" ]; do
        printf "The server will stop in %02d:%02d:%02d" $((math/3600)) $(((math/60)%60)) $((math%60))
        sleep 1
        clear
    
        # Reset count down using current time;
        # An alternative is to decrease 'math' value but that
        # may be less precise as it doesn't take the loop execution into account
        time=$(date +"%s")
        math=$((execute-time))
    
    if [ "$time" == "$execute" ]; then
    
    break
    fi 
    
    done 
    
    echo "Cycle has ended"
    
    • 1

相关问题

  • 如何每 5 秒运行一次脚本?

  • 如何将必须从其自己的目录中运行的程序添加到面板或主菜单?

  • 如何编写 shell 脚本来安装应用程序列表?

  • Mac OS X Automator 的替代品?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve