1

I want to write a greeting script. What I have so far:

if [ "$HOUR" -lt 12 ]; then
    echo " Good Morning Mohamed"
elif [ "$HOUR" -ge 12 ] && [ "$HOUR" -lt 16 ]; then
    echo " Good Afternoon Jama."
elif [ "$HOUR" -ge 16 ]; then
    echo " Good Evening Jama."  
fi

This give me an error

integer expression expected

How can I fix this?

5
  • HOUR is the empty string. See the duplicate. Commented Mar 7, 2017 at 3:27
  • So what do i do, do i have to declare a string variable. Commented Mar 7, 2017 at 3:35
  • You have to assign some numeric value to HOUR. It's either empty or a string, but it's impossible to tell without the rest of your script. If this is your complete script, then it's obviously the empty string. Commented Mar 7, 2017 at 3:38
  • If you don't want to set HOUR and want the code to still work, you can use if (( HOUR > 12 )); then ... construct. Look at this post for more details: stackoverflow.com/a/8552128/6862601 Commented Mar 7, 2017 at 3:41
  • brackets ain't working. Commented Mar 7, 2017 at 6:36

1 Answer 1

0

As the comments indicate, it works if you just give HOUR a value (at least on my CentOS 6 system):

HOUR=`date +%k`
if [ "$HOUR" -lt 12 ]; then
    echo " Good Morning Mohamed"
elif [ "$HOUR" -ge 12 ] && [ "$HOUR" -lt 16 ]; then
    echo " Good Afternoon Jama."
elif [ "$HOUR" -ge 16 ]; then
    echo " Good Evening Jama."  
fi
Sign up to request clarification or add additional context in comments.

Comments