LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   howto: bash.org quotes at logon (https://www.linuxquestions.org/questions/linux-general-1/howto-bash-org-quotes-at-logon-703564/)

jbarelds 02-10-2009 04:38 AM

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!

Juako 03-25-2010 05:11 PM

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

Juako 03-26-2010 12:30 AM

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

jbarelds 03-26-2010 03:42 AM

Quote:

Originally Posted by Juako (Post 3912826)
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!

Juako 03-26-2010 12:15 PM

Quote:

Originally Posted by jbarelds (Post 3912981)
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 :hattip: 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 :()

MTK358 03-26-2010 02:03 PM

Why not use the wget utility:

Code:

wget http://bash.org/?random1 -O -
The above will output to stdout.

Juako 03-26-2010 02:34 PM

Quote:

Originally Posted by MTK358 (Post 3913590)
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 :)


All times are GMT -5. The time now is 10:50 AM.