LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Date Extraction (https://www.linuxquestions.org/questions/programming-9/date-extraction-4175477351/)

Lucien Lachance 09-16-2013 03:36 PM

Date Extraction
 
I'm trying to set up a script to run on a cron job to output a different message from Monday-Friday. Instead of echoing default messages like so, I'd like to be able to output a large list of random sentences or phrases from an array. Is there a better way to do this without making a mess?

Code:

#!/bin/bash

week_day=$(date +"%u")

case "$week_day" in
  1 )  echo 'Wake up, now!' ;;
  2 )  echo 'Dude, GTA V!'  ;;
  3 )  echo 'Hump-day!' ;;
  4 )  echo 'Almost there...' ;;
  5 )  echo 'Finally!!!' ;;
esac


dwhitney67 09-16-2013 03:49 PM

Use a hash-table.
Code:

#!/bin/bash

declare -A motd=(
        [1]="Wake up now!"
        [2]="Dude, GTA V!"
        [3]="It's Hump-day!"
        [4]="Almost there...!"
        [5]="Finally!!!"
    )

week_day=$(date +"%u")

echo ${motd[$week_day]}


Lucien Lachance 09-16-2013 03:58 PM

I can't believe I didn't think of that! Nicely done.

Habitual 09-16-2013 04:12 PM

speaking of Hump Day!


All times are GMT -5. The time now is 09:23 PM.