使用 bash 脚本检查时间戳是否在特定范围内

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2905259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:08:05  来源:igfitidea点击:

check if timestamp is in certain range using bash script

linuxbashscripting

提问by vehomzzz

Say I have a bunch of time stamps

说我有一堆时间戳

As I iterate over these timestamps,

当我遍历这些时间戳时,

01:23:00
12:34:14
17:09:12
...

I want to include only timestamps between 08:00:00 and 17:00:00

我只想包含 08:00:00 和 17:00:00 之间的时间戳

please suggest

请建议

回答by Paused until further notice.

You can do a simple string comparison:

你可以做一个简单的字符串比较:

if [[ "$timestamp" > "08:00:00" && "$timestamp" < "17:00:00" ]]

If you want to include the ends of your range, you'll have to test for that separately since Bash doesn't have a >=or <=operators for strings:

如果要包含范围的结尾,则必须单独测试,因为 Bash 没有用于字符串的a>=<=运算符:

start="08:00:00"
end="17:00:00"
if [[ "$timestamp" == "$start" ||
      "$timestamp" >  "$start" && "$timestamp" <  "$end" ||
                                  "$timestamp" == "$end" ]]