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?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • `HOUR` is the empty string. See the duplicate. – Benjamin W. Mar 07 '17 at 03:27
  • So what do i do, do i have to declare a string variable. –  Mar 07 '17 at 03: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. – Benjamin W. Mar 07 '17 at 03: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: http://stackoverflow.com/a/8552128/6862601 – codeforester Mar 07 '17 at 03:41
  • brackets ain't working. –  Mar 07 '17 at 06:36

1 Answers1

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
Leslie
  • 618
  • 4
  • 14