It seems like Iâ€™m writing a lot of fizzbizz examples at the moment. It is kind of fun experimenting with different languages. There are always different constructs for looping and recursing. Iâ€™m also super happy that there are a lot of languages to get through before I have to write it in prolog. I have prettyÂ traumaticÂ memories of prolog from university. Todayâ€™s language is shell script.
Of course shell script isnâ€™t just one language, it depends on which shell youâ€™re using. When I worked a lot with unix derivatives I mostly worked with bash scripting. Unless it was aÂ particularlyÂ old or odd OS in which case we would end up on plain sh. There are a bunch of other shells out there and I can remember a time when both csh and ksh were also popular. I can also remember when druids sacrificed goats. Is there a link between kshâ€™s stupidÂ arcaneÂ syntax and goat slaughter? I canâ€™t proveÂ conclusivelyÂ that there is but there are no dead goats here and no ksh syntax either. Draw your own conclusions.
I thought I would try using zsh style scripting for fizz bizz. Zsh is a newer shell which has many of the features of bash and also borrows from other shells. Now I say â€œnewerâ€ but it still dates to 1990.
The script starts with a sha-bang which instructs the program loader that to run the script it should execute /bin/zsh which is where zsh lives on my machine. It might be better to replace it with #!/usr/bin/env zsh which instructs the program loader to launch env which searches for zsh. This allows for searching of the path for zsh. There is an increased security risk with doing so as an alternate zsh might be selected. However this risk is probably worth it for increased portability.
I was hoping that it was possible to putÂ mathematicalÂ expressions in the case statements but thatâ€™s not possible. Â Instead I took advantage of case statements here and the fact that we can take the remainder modulo 6 to do most of the fizz bizz heavy lifting. On line 3 there is a bit of a syntactic oddity. Zsh is not really designed for doing a lot of math so arithmetic operations need to live inside double parenthesis.
Gosh, I canâ€™t wait for the next interview where Iâ€™m asked about fizz bizz. I am going to kill that question.