Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
Slashdot.org

Journal James A. A. Joyce's Journal: An Experimental Shell Script For FP Whores 17

#!/bin/bash

#
# slashbot
#
# A shell script for automatically and constantly
# checking the Slashdot front page for new stories
# in order to make getting the FP easier.
# Written in bash shell script because Perl sucks.
# Hopefully Slashdot doesn't fuck this up.
#
# How to use it? Just download it into a directory
# and run it. It doesn't need root access; it just
# makes a couple of temporary files in /tmp but it
# cleans up after itself, even after a Ctrl-C.
# It works as a run-forever daemon, and it
# should send out a message via 'wall' when a new
# story pops out; that part is untested.
#

# naptime: how long to sleep for between front page grabs in seconds
if [ "$1" ]
then
    NAPTIME="$1"    # first argument is naptime in seconds
else
    NAPTIME=60
fi

############################
# clean_die()
############################
function clean_die()
{
    rm -f /tmp/slashbot_listing[12].txt

    if [ "$1" != "" ]
    then
        exit $1
    else
        exit 0
    fi
}

############################
# get_front_page_titles()
#    This function outputs to standard output the
#    story titles listed on the "light" cut-down
#    version of the slashdot.org front page: it's
#    about 5-10KB; good for modem users and good for us!
############################
function get_front_page_titles()
{
    curl --max-time 40 'http://slashdot.org/index.pl?light=1' 2> /dev/null | \
        grep -o '<H2>.*</H2>' | \
        sed 's/<A HREF=.*\/\">//g' | \
        sed 's/<\/A>//g' | \
        grep -v '<H2>News for nerds, stuff that matters</H2>' | \
        sed 's/<H2>//g' | \
        sed 's/<\/H2>//g'
}

############################
# new_story_alert()
############################
function new_story_alert()
{
    wall slashbot: new Slashdot story: \
        \'$(head -1 /tmp/slashbot_listing2.txt)\'
}

trap 'clean_die' 2 3

# main program loop below

while [ 1 ]
do

    echo "Getting front page"
    get_front_page_titles > /tmp/slashbot_listing1.txt
    if test -a /tmp/slashbot_listing2.txt
    then
        echo "Comparing to old copy"
        diff /tmp/slashbot_listing[12].txt > /dev/null
        if [ "$?" == 1 ]
        then
            new_story_alert
        fi
    else
        echo "Notice: no old copy to compare"
    fi

    echo "Notice: sleeping for $NAPTIME seconds"
    sleep $NAPTIME
    echo "Notice: done waiting"

    echo "Getting front page"
    get_front_page_titles > /tmp/slashbot_listing2.txt
    echo "Comparing to old copy"
    diff /tmp/slashbot_listing[12].txt > /dev/null

    if [ "$?" == 1 ]
    then
        new_story_alert
    fi

    echo "Notice: sleeping for $NAPTIME seconds"
    sleep $NAPTIME
    echo "Notice: done waiting"

done
This discussion has been archived. No new comments can be posted.

An Experimental Shell Script For FP Whores

Comments Filter:

Get hold of portable property. -- Charles Dickens, "Great Expectations"

Working...