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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-10-2009, 04:38 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2009
Posts: 9
Rep:
|
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!
|
|
|
|
03-25-2010, 05:11 PM
|
#2
|
|
Member
Registered: Mar 2010
Posts: 202
Rep:
|
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
|
|
|
|
03-26-2010, 12:30 AM
|
#3
|
|
Member
Registered: Mar 2010
Posts: 202
Rep:
|
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
|
|
|
|
03-26-2010, 03:42 AM
|
#4
|
|
LQ Newbie
Registered: Jan 2009
Posts: 9
Original Poster
Rep:
|
Quote:
Originally Posted by Juako
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!
|
|
|
|
03-26-2010, 12:15 PM
|
#5
|
|
Member
Registered: Mar 2010
Posts: 202
Rep:
|
Quote:
Originally Posted by jbarelds
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  )
|
|
|
|
03-26-2010, 02:03 PM
|
#6
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Why not use the wget utility:
Code:
wget http://bash.org/?random1 -O -
The above will output to stdout.
|
|
|
|
03-26-2010, 02:34 PM
|
#7
|
|
Member
Registered: Mar 2010
Posts: 202
Rep:
|
Quote:
Originally Posted by MTK358
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 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|