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

Grab a random fortune from thinkgeek UNIX
No practical purpose to this tip, but it does have entertainment value. This little shell script fetches and parses a fortune from the thinkgeek site. Enjoy!
# fetch a fortune from thinkgeek

curl -s | \
sed -n '/(refresh for another)/,/table\>/p' | \
sed -n '/<p>/,/<\/p>/{/[.]/p; }' | \
sed '{/^<[/]*p>/d; s/<[BbRr]*>//g; s/<\;/</g; s/>\;/>/g; }'
Occassionally a blank one is returned; just try again.

[Editor's note: Make sure you make the script executable (chmod 755 script_name), and this worked for me when tested (and some of them are quite humorous).]
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)

Grab a random fortune from thinkgeek | 13 comments | Create New Account
Click here to return to the 'Grab a random fortune from thinkgeek' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
The URL works in a web browser as well.
Authored by: Lecter on Feb 07, '03 11:03:34AM
You can also just got to the webpage if you aren't comfortable with the terminal: L

[ Reply to This | # ]
use wget -q
Authored by: mrgerbek on Feb 07, '03 11:47:20AM

I'm on a Solaris 8 box at work, and we don't have curl (or much else) installed. I've substituted wget -q in place of Curl, though the sed calls don't seem to work correctly. I guess I'll have to wait until I get home.

[ Reply to This | # ]
Authored by: natenate on Feb 07, '03 12:32:18PM
Why not just install fortune?

[ Reply to This | # ]
Authored by: alys on Jul 17, '04 11:59:45PM
The ThinkGeek fortunes aren't the same as the fortune fortunes. :) This is from the ThinkGeek page that that script downloads:
"Customer Fortunes. So, you wanna see what people say aboot us, eh? A lot of people send in their comments and thoughts, not only about our site and our products, but about a very wide variety of things. This page is our local fortune file that will randomly select one of the many customer-submitted comments and present it to you for your reading pleasure (or horror, as the case may be). "
It's a fun collection.
"Hurry up! Climb into this bag before the yaks get here!"

[ Reply to This | # ]
Authored by: bluehz on Feb 07, '03 01:46:45PM

I was playing with this one today (I try damn near every tip!) and I used to have Fortune - I used it for a while to create rand sigs for my Eudora. For some reason though - I don't have it anywhere now. Did not find with 'which fortune' or locate fortune (I keep my db updated everynight - I know you were going to ask). So at some point fortune got obliterated. I just reinstalled from fink..

fink install fortune-mod

[ Reply to This | # ]
Set in .cshrc
Authored by: vaiism on Feb 07, '03 02:05:54PM

I set this to run in my .cshrc and it is pretty kewl with as many terms as I open on a constant basis to read some of the random stuff on here.

Pretty kewl hint...


[ Reply to This | # ]
.cshrc is the wrong place to put it
Authored by: breen on Feb 07, '03 07:00:07PM

If you run fortune from .cshrc (or .bashrc) you'll get a fortune with all shell instances -- whether it's an interactive login or not.
This has the unfortunate side effect of breaking valuable tools like rsync over an ssh connection, because the extra output from the remote shell causes an error.

Fortune and its kin should be invoked from a .profile file of some sort. (I'm not at my OS X box right now -- what's the correct file for tcsh?

[ Reply to This | # ]
.cshrc is the wrong place to put it
Authored by: SeanAhern on Feb 08, '03 12:02:53AM


[ Reply to This | # ]
Handle DOS Line Endings
Authored by: kerbaugh on Feb 09, '03 01:48:58AM

In its raw form, the html has DOS line endings, which makes it hard to manipulate via UNIX tools. My preferred version of a script to display just the fortune part of the page would strip off the carriage return first, thus converting to UNIX line endings. Here's the script I would use for this purpose:

# fetch a fortune from thinkgeek

curl -s |
sed -n '/(refresh for another)/,/table\>/{

It must be noted however, that the carriage return, depicted here as it would appear on the command line, "^M" is a literal control character and must be entered with a tool that can do this. To my knowledge, only UNIX text editors can do it. I'd love to know how to make BBEdit do it, if anyone knows how. In a UNIX text editor, this character is entered with the key sequence, <Control>-v <Control>-m. I've included a substitution to convert escape sequences for "greater than" and "less than". One can add more as the need arises. I don't understand the author's s/<\;/</g and s/>\;/>/g substitutions but they may eventually become evident. If anyone knows the reason for them, please let me know. As an aside, my script has yet to return a blank line.

[ Reply to This | # ]
Handle DOS Line Endings
Authored by: kal on Feb 09, '03 05:43:49AM

>In its raw form, the html has DOS line endings, which
>makes it hard to manipulate via UNIX tools.

Not if you use the right unix tool :-) The unix "tr" (translate) command should help you get rid of the carriage return character. For instance on the file test:

cat test | tr -d "\r"

Should remove all carriage returns from the file test and print it to STDOUT.

[ Reply to This | # ]
Handle DOS Line Endings
Authored by: kerbaugh on Feb 09, '03 08:57:24AM
   You're right of course. What I should have said was that it "makes it difficult to handle with UNIX tools unless they are removed". I couldn't figure out at first why my matches weren't matching. All UNIX text editors have convenient means of deleting things; I just used sed to do it since I was already in sed.
   Line Printer paper is strongest at the perforations.

[ Reply to This | # ]
Avoid timeouts
Authored by: kal on Feb 12, '03 05:52:36AM

For those that use this in .profile/.login or whatever.. You might want to use the -m option for curl (max time in seconds for the operation). This way you won´hang "forever" if the connecion is down ...

curl -s -m 5 .......

[ Reply to This | # ]
Grab a random fortune from thinkgeek
Authored by: dontlikehippies on Jul 16, '04 04:40:48AM

after you get the script going, toss it into geektool for a good time...

[ Reply to This | # ]