Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Show a random message at bash shell startup UNIX
I really don't know what I'm doing here (my experience in shell scripting amounts to about two hours), but I put together this little script that will print out a random message each time you open a new bash window. Just copy this into your user's .bash_profile file, located in your user's home directory:
fn=/path/to/quotes.txt
cnt=$(wc -l $fn)
lns=${cnt:6:2}
if [ "$lns" = 0 ]; then
  rnd=1
else
  let "lns += 1"
  rnd=$(expr $RANDOM % $lns)
  let "rnd += 1"
fi
sed -n ${rnd}p $fn
Set the fn variable in the first line to the path and name of your quotes file. Each quote should be on its own line, with no extra lines between the quotes.

[robg adds: This worked for me -- note that this will not replace Terminal's welcome message (Welcome to Darwin!) with the quote -- it shows up after the welcome message. If you want to change the welcome message, you need to edit /etc/motd. Replacing the welcome message with a random quotation is left as an exercise for the reader (i.e. I don't have a clue...), but this hint might get you started.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[14,920 views]  

Show a random message at bash shell startup | 8 comments | Create New Account
Click here to return to the 'Show a random message at bash shell startup' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Show a random message at bash shell startup
Authored by: mankoff on May 14, '07 07:37:01AM

This script is nice and all, but why re-invent the wheel? The 'fortune' program has been around since 1979, so I'm guessing they've fixed any bugs and implemented almost all the major features during the past 30 years. Why not just use fortune and your own custom fortune file?

---
http://spacebit.dyndns.org



[ Reply to This | # ]
Show a random message at bash shell startup
Authored by: tdknox on May 14, '07 08:21:29AM
I have already written a fortune program for Mac OS X. The program (along with all source code and several fortune files) can be downloaded from Mac OS X Fortune

Tom

[ Reply to This | # ]

Show a random message at bash shell startup
Authored by: lras on May 14, '07 10:31:20AM
... or a little shorter (to save the environment :-)
fn=/path/to/quotes.txt 
sed -n $(awk "END{ print $RANDOM%NR+1}" $fn)p $fn


[ Reply to This | # ]
No need for sed
Authored by: mzso on May 14, '07 11:32:51PM
You can go even more environment friendlier, than that ... :-) No need for sed if you already start awk ... and no need to read the file twice.
awk "{ lines[NR] = $0 } END { print lines[$RANDOM % NR + 1] }" /path/to/quotes.txt

And if you're interested in various other solutions (eg. Perl :-) ), then check out this thread.

[ Reply to This | # ]
No need for sed
Authored by: mzso on May 14, '07 11:37:31PM

Of course one should choose the solution that suites her/him better ... eg. if you've got a quotes.txt of some GB in size, then caching the lines in an array might make your os swap a little bit ... ;-)



[ Reply to This | # ]
Show a random message at bash shell startup
Authored by: emont88 on May 14, '07 08:41:41PM

thanks for the comments and the shorter version.
as I said, I really don't know what I'm doing here - this was really just the first thing I came up with as I was teaching myself bash scripting



[ Reply to This | # ]
Show a random message at bash shell startup
Authored by: S Barman on May 14, '07 08:42:31PM
Back in the olden days, we used to have /usr/bin/fortune to do just this. This program was put into our .profile or .login to print right before the prompt. It was a single program that would read a specific file, usually /usr/lib/fortunes, and/or ~/.fortunes. There was even an "obscene" mode (-o) that would include those fortunes that were considered obscene in the mix.

I found an open source version of /usr/bin/fortune here. Using this program may be easier than a shell/awk/perl/php/etc script!

[ Reply to This | # ]

Show a random message at bash shell startup
Authored by: EddEdmondson on May 15, '07 08:49:00AM

You've linked to Linux versions!

Anyway, fortune's available through the usual Fink or MacPorts methods or you can no doubt track down the source and compile it that way instead.



[ Reply to This | # ]