Shell – Generate line number / “index” within a range from a date seed value

dategrepshell-script

I'm trying to create a bash script to display a word of the day (random for each day). I have a dictionary file that on each line has a word and its definition.

I'd like to use date to get a unique value for each day. Like so

today=$(date '+%Y%m%d') # will return 20160616 (for today)

Now I'd like to use this value to generate a line number for me to grab from the dictionary file.

My dictionary is 86036 lines long so I need to convert $today to a value between 1 and 86036.

What is the best way to do this?

Best Answer

A somewhat different solution: Scramble the lines in your text files with a cron job every day. Your script then picks the first line.

Cron job (requires a sort that can "sort" data randomly with -R):

0 0 * * * sort -R -o wotd_data.txt wotd_data.txt

Or, if your cron understands @daily (see man 5 crontab):

@daily sort -R -o wotd_data.txt wotd_data.txt

Script:

head -n 1 wotd_data.txt

Full paths to wotd_data.txt are obviously required.

Related Question