LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-10-2009, 04:38 AM   #1
jbarelds
LQ Newbie
 
Registered: Jan 2009
Posts: 9

Rep: Reputation: 1
Talking howto: bash.org quotes at logon


Hi all,

Many ppl like to have a fortune cookie displayed at console logon, which can be done by installing the 'fortune' package. But here's an alternative, displaying a random IRC quote from bash.org, with a 'funny' rating above 0.

Prerequisites:
- Internet connection
- coreutils, gawk, grep, links
- change the $workdir to your liking

Here's the script to get a list of URL's pointing to random bash.org quotes:
Code:
#!/bin/bash

# Meant to by run by cron, once a month would be fine.
# Prerequisites:
# gawk, grep, links

# by Jan Barelds, 02-09-2009

workdir=/root/scripts/randomquote
links -source http://bash.org/?random1 | grep 'Permanent link' | gawk -F ? '{print $2}' | gawk -F \" '{print "http://bash.org/?"$1}' >$workdir/Quotes.URLS
Here's the script to display a quote:
Code:
#!/bin/bash

# Prerequisites:
# coreutils, gawk, grep, links

# Add this script to the login profile to show a random bash.org quote on each login screen:
# /etc/profile                  - for all users
# ~/.profile or ~/.bash_profile - for just you

# by Jan Barelds, 02-09-2009

workdir=/root/scripts/randomquote
ping -c 1 bash.org >/dev/null 2>&1
rc=$?
if [ $rc -ne "0" ]; then
        echo ERROR! - Quotes unavailable, could not connect to bash.org.
else
        quotenum=`cat -n $workdir/Quotes.URLS | tail -1 | gawk '{print $1}'`
        selectquote=$(($RANDOM%$quotenum+1))
        quoteurl=`cat -n $workdir/Quotes.URLS | grep -w $selectquote | gawk '{print $2}'`
        links -dump $quoteurl | grep -v 'Add Quote' | grep -v 'Hosted by' | grep -v 'QDB 1999-2009' | grep -v 'Search / ModApp' | grep -v 'quotes approved' | grep -v 'QDB Admin' | grep -v 'Paypal Donate' | grep -v '[X]' | grep -v '^$'
        echo
fi
Have fun!
 
Old 03-25-2010, 05:11 PM   #2
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Hey jbarelds, it works like a charm, thank you! I enhanced the filter with a sed oneliner instead of more pipes so it outputs only the quote and runs faster. Here:

Code:
# Prerequisites:
# coreutils, gawk, grep, links

# Add this script to the login profile to show a random bash.org quote on each login screen:
# /etc/profile                  - for all users
# ~/.profile or ~/.bash_profile - for just you

# by Jan Barelds, 02-09-2009

workdir=/usr/local/bin/randomquote
ping -c 1 bash.org >/dev/null 2>&1
rc=$?
if [ $rc -ne "0" ]; then
        echo ERROR! - Quotes unavailable, could not connect to bash.org.
else
        quotenum=`cat -n $workdir/Quotes.URLS | tail -1 | gawk '{print $1}'`
        selectquote=$(($RANDOM%$quotenum+1))
        quoteurl=`cat -n $workdir/Quotes.URLS | grep -w $selectquote | gawk '{print $2}'`
        links -dump $quoteurl | sed -n '/^.*[X].*$/,/^.*Home.*Latest.*$/{
/^.*[X].*$/d
/^.*Home.*Latest.*$/d
p
}' 
fi
Cheers

Last edited by Juako; 03-25-2010 at 05:13 PM. Reason: forgot to put in codeblock
 
Old 03-26-2010, 12:30 AM   #3
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Better

In a frantic state of sed addiction i've polished it to a better version yet:

Code:
#!/bin/bash

quotefile="${0%/*}/Quotes"
#uncomment to get funny modded quotes
#funny=1

if [ "$1" == "gimmemore" ]; then
  ping -c 1 bash.org &> /dev/null
  [ $? == "0" ] && elinks -dump http://bash.org/?random$funny | sed -n '/^.*ModApp.*_____$/,/^.*Home.*Latest.*$/{/^.*ModApp.*_____$/d;p;}'>$quotefile
  exit 0
fi

quote=$[$RANDOM%$[$(sed -n '/^.*\]#.*$/p' "$quotefile" | wc -l)-2]+1]
quote=$(bash -c "cat -n \"$quotefile\" | sed -n '/^.*\]#.*$/p' | sed -n \"${quote}p\" | sed 's/^ *\([0-9]\+\).*$/\1/g'")
sed -n "$quote,/^.*\]#.*$/{$quoted;/^.*\]#.*$/d;p}" $quotefile
crontab to fetch more quotes
1 */2 * * * /bin/bash -c /home/me/scripts/randomquote/bash.org gimmemore

and in .bashrc/.profile/profile, well just
/home/me/scripts/randomquote/bash.org

It will connect only to grab a page o'quotes. The delay before getting the prompt was starting to getting on my nerves

EXPLANATION
with "gimmemore" it fetches the page with all the quotes, and filters everything before and after the quotes, leaving only the entries and between them certain strings that appear after every quote (you can see them in the saved file). I'll use those "special lines" as markers.

when you call it w/o arguments it
1) count the total number of markers (aka number of quotes+1), pick a number between 1 and total markers-1, and saves it in $quote
2) print the quotefile with linenumbers, filtering only markers, filtering again for the one i've picked, and saves the *real* line number again in $quote
3) finally it prints everything in between that linenumber and the next marker

Last edited by Juako; 03-26-2010 at 02:07 AM. Reason: polishin code
 
Old 03-26-2010, 03:42 AM   #4
jbarelds
LQ Newbie
 
Registered: Jan 2009
Posts: 9

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Juako View Post
In a frantic state of sed addiction i've polished it to a better version yet:

Code:
#!/bin/bash

quotefile="${0%/*}/Quotes"
#uncomment to get funny modded quotes
#funny=1

if [ "$1" == "gimmemore" ]; then
  ping -c 1 bash.org &> /dev/null
  [ $? == "0" ] && elinks -dump http://bash.org/?random$funny | sed -n '/^.*ModApp.*_____$/,/^.*Home.*Latest.*$/{/^.*ModApp.*_____$/d;p;}'>$quotefile
  exit 0
fi

quote=$[$RANDOM%$[$(sed -n '/^.*\]#.*$/p' "$quotefile" | wc -l)-2]+1]
quote=$(bash -c "cat -n \"$quotefile\" | sed -n '/^.*\]#.*$/p' | sed -n \"${quote}p\" | sed 's/^ *\([0-9]\+\).*$/\1/g'")
sed -n "$quote,/^.*\]#.*$/{$quoted;/^.*\]#.*$/d;p}" $quotefile
crontab to fetch more quotes
1 */2 * * * /bin/bash -c /home/me/scripts/randomquote/bash.org gimmemore

and in .bashrc/.profile/profile, well just
/home/me/scripts/randomquote/bash.org

It will connect only to grab a page o'quotes. The delay before getting the prompt was starting to getting on my nerves

EXPLANATION
with "gimmemore" it fetches the page with all the quotes, and filters everything before and after the quotes, leaving only the entries and between them certain strings that appear after every quote (you can see them in the saved file). I'll use those "special lines" as markers.

when you call it w/o arguments it
1) count the total number of markers (aka number of quotes+1), pick a number between 1 and total markers-1, and saves it in $quote
2) print the quotefile with linenumbers, filtering only markers, filtering again for the one i've picked, and saves the *real* line number again in $quote
3) finally it prints everything in between that linenumber and the next marker
I'm not that good with sed, so thanks a lot for your reply. The exclusion of lines using grep -v wasnt a good idea anyway, for example because bash.org now displays "QDB 1999-2010", though the script tries to exclude "QDB 1999-2009".

Your second take is basically a new script, looks good!
 
Old 03-26-2010, 12:15 PM   #5
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Quote:
Originally Posted by jbarelds View Post
I'm not that good with sed, so thanks a lot for your reply. The exclusion of lines using grep -v wasnt a good idea anyway, for example because bash.org now displays "QDB 1999-2010", though the script tries to exclude "QDB 1999-2009".

Your second take is basically a new script, looks good!
Glad you like it. I suck with awk that's why i used sed, which i'm starting to understand lately and it's a lot of fun.

Yes i'm using the new one now and it's good. It'd be nice to add more things, like filters, autoconfig, exporting to "fortune" and whatnot.

I think $random can have random values also, and there also other params in the site that can be used as options.

I suggest that changes or variations of any kind keep being posted to this thread as an aid to the poor unix admins of the world staring at prompt most of their time

(like this one )
 
Old 03-26-2010, 02:03 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Why not use the wget utility:

Code:
wget http://bash.org/?random1 -O -
The above will output to stdout.
 
Old 03-26-2010, 02:34 PM   #7
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Quote:
Originally Posted by MTK358 View Post
Why not use the wget utility:

Code:
wget http://bash.org/?random1 -O -
The above will output to stdout.
That would definitely be better, in fact i tried it at first but stayed with elinks cause it already parses the html entities.

But they can get filtered with sed though, and <p class="quote"></p> might be a better wrapper too. I'm at my work right now, but i'll try that later. Of course, if you feel like it, would kick to see the mod already posted
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - double quotes don't protect Exclamation marks geoff_f Programming 15 04-15-2007 09:10 PM
bash-2.05b$ instead of user@domain after upgrading...also...no more quotes. RoaCh Of DisCor Slackware 3 12-16-2004 11:17 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 12:04 PM
How to get rid of quotes in bash? CodeWarrior Slackware 7 05-21-2003 09:02 PM


All times are GMT -5. The time now is 07:41 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration