0

I'm trying to write a simple script to check if the current hour is before or after 12:00 AM.

#!/bin/bash
var1=`date +"%T"` # error "not an integer"
var2='12:00:00'
if [ $var1 -lt $var2 ]; then # always false
    echo "Good morning!"
else
    echo "Good afternoon!"
fi

The error says that var1 must be an integer number, and the condition is always false. I would like some help please. Thank you very much.

  • Suggestion: var1=$(date + "%T") – Paulo Tomé Dec 17 '19 at 9:56
3

The test you are using can only compare integer numbers. date +%T produces strings containing :, so your test fails.

You could use date +%H and set var2=12.

Or you could use date +%H%M%S and set var2=120000.

That being said, I'd suggest you use $(date ...) instead of the backticks. It's much easier to read and allows you to wrap multiple commands into one.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.