1

I am having trouble with using a case statement to return months. It feels so obvious I'm not sure why its stumping me so well!

month=(01 02 03 04 05 06 07 08 09 10 11 12)
case ${month[@]} in
   test ${month[@]:8:1} = $(date +%m))
   RPROMPT='September %D{%d %Y} %@'
   ;;
   *)
   RPROMPT='%D{%b %d, %Y} %@'
   ;;
esac

Yes. I do want the full name of the months, not just a three-letter abbreviation.

It's also a good refresher as I've not touch shell-script in years.

It returns the *) case.

1
  • The syntax is wrong (see harrymc's answer or have a look at zsh.sourceforge.io/Doc/Release/…) If you want to include the month in your prompt, why not just something like that: month=(January February March April May June July August September ...); RPROMPT="$month[$(date +%m)]" ?
    – mpy
    Sep 12, 2022 at 16:51

1 Answer 1

0

You need to loop over the array and apply the case statement to each element in turn:

for element in "${month[@]}"; do
  case ${element} in
3
  • Why? I wrote the code like I did to avoid looping and so I could just call ${month[@]:8:1} or ${month[@]:2:1} and just set a case to September or February. Sep 12, 2022 at 9:23
  • so I could make a case statement and then call ${month[@]:8:1} and print "September". I do have an if statement that works but I thought a case statement would be more appropriate for what I'm trying to do. Sep 12, 2022 at 9:30
  • you can't just test for equality? test ${month[@]:8:1} = $(date +%m)) This should test if 09 = 09 without need for a loop or if statement. As it is presently as September. Sep 12, 2022 at 9:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .