LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2013, 03:36 PM   #1
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Rep: Reputation: Disabled
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

Last edited by Lucien Lachance; 09-16-2013 at 03:58 PM.
 
Old 09-16-2013, 03:49 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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]}
 
Old 09-16-2013, 03:58 PM   #3
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
I can't believe I didn't think of that! Nicely done.
 
Old 09-16-2013, 04:12 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
speaking of Hump Day!
 
  


Reply

Tags
bash, linux, shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 08:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration