How can I compare the hour to a string/number in bash? -
we have server use run selenium tests our extension (for chrome , firefox). run selenium tests every 2 hours. want run different tests after 14:00 before 14:00. have variable:
start_hour=`tz='asia/tel_aviv' date +"%h"` and know how compare specific hour, such 08:00 (it's string):
if [ "$start_hour" = "08" ]; ... fi but how check if variable shows hour after 14:00 (including 14:00), or before 14:00? can compare strings in bash , how? want check if $start_hour >= "14", or not?
is answer different if want check after 08:00 or before?
to perform greater or less-than comparisons, test operations -gt, -le, -ge, , -le exist.
start_hour=$(tz='asia/tel_aviv' date '+%k') [ "$start_hour" -ge 8 ] note use of %k vs %h, , 8 vs 08 -- leading 0 can prevent numbers being interpreted decimal.
similarly, in native bash syntax, numeric comparison:
start_hour=$(tz='asia/tel_aviv' date '+%k') (( start_hour >= 8 )) if shell bash, , not /bin/sh, , want ascii-sort string comparison, can use > , < inside of [[ ]]:
start_hour=$(tz='asia/tel_aviv' date '+%h') [[ $start_hour > 08 || $start_hour = 08 ]]
Comments
Post a Comment