开发者

check if timestamp is in certain range using bash script

开发者 https://www.devze.com 2022-12-31 19:17 出处:网络
Say I have a bunch of time stamps As I iterate over these timestamps, 01:23:00 12开发者_开发问答:34:14

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

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:

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

精彩评论

暂无评论...
验证码 换一张
取 消