0

I am using a script to compare time but getting the error, Quarterly_File.sh: line 139: [[: 0148: value too great for base (error token is "0142"). Is there any way I could change my inputs in time (hour) format and compare it with other time? My inputs are as follows.

startTime=00:45
endTime=01:30
fileTime=01:42

I am simply comparing them by removing : between them, like below.

if (0142 -ge 0045 && 0142 -lt 0130)
    then 
    // my logic
fi

Note that time range could be anything within a one-hour range.

Examples

0000    0045,
0030    0130,
2345    0014 (of next day)
2
  • You are not comparing time, from the docs "Constants with a leading 0 are interpreted as octal numbers". Commented Oct 24, 2019 at 14:57
  • Anyway, you have to convert the dates to seconds and then perform the ops. Commented Oct 24, 2019 at 15:20

2 Answers 2

1

Not sure how you're going to use ( and ) with bash operators however this would work:

if [ 0142 -ge 0045 ] && [ 0142 -lt 0130 ]; then
    echo "yep"
fi

If that's not what you're looking for, please paste as much relevant code as possible.

edit:

As pointed out in the comments below, this would indeed cause issues as it's comparing octal numbers to (eventually after 10am) decimal numbers

I would recommend using date to convert time to seconds, then compare that.

An example would be:

# grab these however you are currently
time1="01:42"
time2="00:45"
time3="01:42"
time4="01:30"

time1Second=$(date -d "${time1}" +%s)
time2Second=$(date -d "${time2}" +%s)
time3Second=$(date -d "${time3}" +%s)
time4Second=$(date -d "${time4}" +%s)

# then your comparison operators and logic:
if [ "$time1Second" -ge "$time2Second" ] && [ "$time3Second" -lt "$time4second" ]; then
    # logic here
    echo true
fi

This way, you are always comparing numbers in the same base

1
  • spot on, cheers. forgot how bash handles octal. Edited Commented Oct 24, 2019 at 15:22
0

Convert the times to minutes and compare those

startTime=00:45
fileTime=01:42

hr=${startTime/:*} mn=${startTime/*:}      # Split hh:mm into hours and minutes
startMins=$(( ${hr#0} * 60 + ${mn#0} ))    # Minutes since midnight

hr=${fileTime/:*} mn=${fileTime/*:}
fileMins=$(( ${hr#0} * 60 + ${mn#0} ))

if [[ $fileMins -ge $startMins ]]; then : ...; fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.