DEV Community

Discussion on: Towards a better message of the day

Collapse
 
taikedz profile image
Tai Kedzierski • Edited

I was trying to figure out why you were using eval, then realised, you are not making use of "array" structures. Using those, you can have:

funky_motd() {
  printer=(cat)
  filter=(cat)

  if command -v toilet >/dev/null; then
    printer=(toilet -t -f future)
  elif command -v figlet >/dev/null; then
    printer=(figlet)
  fi

  if command -v lolcat >/dev/null; then
    filter=(lolcat -S "$(hostname | shasum | tr -d '[a-f]' | cut -b-4)")
  fi

  printf "\n"
  echo "$*" | "${printer[@]}" | "${filter[@]}" # Using quotes and array dereference
  printf "\n"
}
Enter fullscreen mode Exit fullscreen mode

Using the quotes arround the array dereference allows it to split just as you had specified initially. For a more direct example:

my_var=(a "b c" d)
for x in "${my_var[@]}"; do echo "--> "$x"; done
Enter fullscreen mode Exit fullscreen mode

Prints

--> a
--> b c
--> d
Enter fullscreen mode Exit fullscreen mode

As for your output -- which program eventually produced it? I like it :D

Collapse
 
moopet profile image
Ben Sinclair

Wait what I wrote this forever ago and didn't even notice I was using eval. Now I'm not sure why I was doing that at all. Probably to get round issues with things like printing messages that start with a dash or something

I rarely if ever deal with arrays in shell scripts - but your changes look like the right thing to do! Thanks.

The final output in mine comes down to hostname | toilet -f future | lolcat . Future is such a nice, small font compared to the "tiny" fonts or figlet/toilet's usual enormous banners :)