Specifying a range within case/esac

Complete Unix beginner here. I basically have this script -

This seems to work fine. I want to try and shorten it by making it something like this -

This isn't working. I think it's probably to do with the zero padding that `date +%H` gives me, but if I use `date +%k`, I get a space at the start of single numbers, so that doesn't work either... Can anyone tell me where I'm going wrong?

Thanks in advance.

Hi.

You could shorten it as such:


time1=`date +%l:%M`

case `date +%H` in
0[0-4]) time2="in the early morning" ;;
0[5-9]|1[01]) time2="in the morning" ;;
1[2-6]) time2="in the afternoon" ;;
1[7-9]) time2="in the evening" ;;
2[0-3]) time2="at night" ;;
esac

echo "The time is$time1 $time2"
1 Like

Thank you so much!

Just one more thing, could you explain what is going on in this line of your script? Specifically, the |1[01] part.

Edit: Actually, just worked it out. Thanks again!

By the way, is there an easy way to get the date command to show the st/nd/rd/th suffix for days?

Looking through man date, I don't see that it has that option.