简体   繁体   English

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

[英]check if timestamp is in certain range using bash script

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 请建议

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没有字符串的>=<=运算符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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