The Chuck Norris Facts Bash Script

Pon un Chuck Norris en tu vida! O mejor aun, pon miles de Chuck Facts en tu consola!

Bored STenyaK Productions presents: chuckfacts.sh!

Chuck Norris for god
Coming this winter to a console in front of you…

#!/bin/bash

# check for parameters
if [ -z $1 ]
then
    echo "Please specify the destination chuck norris facts file."
    echo "E.g.: $0 ~/.chuckfacts.txt"
    exit
fi
ffile=$1

old=0
if [ -s $ffile ]
then
    old=$(wc -l $ffile |sed "s/\s.*//g")
fi

echo -n "Getting facts pack"
# download the 170 first chuck norris pages of 4q.cc site to disk
for i in $(seq 1 170)
do
    echo -n " $i"
    # only store facts
    wget -qO - \
       "http://4q.cc/index.php?pid=listfacts&person=chuck&page=$i" \
        | grep "index.php?pid=fact&person=chuck" >> $ffile
done
echo ""

echo -n "Post-processing facts..."
# remove unnecessary html code
perl -pi -e "s/.*id=.{32,32}\">//g;s/<\/a>.*//g" $ffile

# replace most common html entities
perl -pi -e "s/&quot;/\"/g" $ffile
perl -pi -e "s/&amp;/&/g" $ffile

# remove empty lines
perl -ni -e "print unless /pid=/" $ffile

# remove redundant lines
cat $ffile |sort |uniq > /tmp/chuckfacts.tmp
mv /tmp/chuckfacts.tmp $ffile
new=$(wc -l $ffile |sed "s/\s.*//g")
echo " OK"
echo "Generated $(($new-$old)) new facts ($new in total) facts."

# show how to add a fortune-like command to bashrc
echo ""
echo "You can add this to your ~/.bashrc file:"
echo 'test -s '$ffile' && cowsay -f $(ls /usr/share/cowsay/cows
      | shuf |head -1) "$(cat '$ffile' |shuf |head -1)"'

Mira que no me aburro a veces ni nada eh…

Tags: , [en, es] | December 18th, 2009 |

6 Responses to “The Chuck Norris Facts Bash Script”

  1. Yuki Says:

    Qué grande! No conocía el “cowsay”, y es muy chulo.

    Lo único, deberías poner el fichero para descargar, o pon un “\” en las dos líneas donde haces el wget, porque al hacer copy-paste eso sale como tres líneas y el wget se queja.

    Yo ya lo he añadido a mi bashrc.

  2. stenyak Says:

    Qué raro, anoche probé a copiarpegar y al ejecutarlo me tiró way…

  3. Paulson Says:

    Wow thats baaaaaaddddd.

  4. Erin Says:

    LOL!!!!!! chuck norris is amazing!!!!!

  5. chuck norris Says:

    its chuck norris can swim through land… leave now

  6. mino Says:

    it would have been beta if it said jesus can walk on water, but chuck norris can swim on land

Leave a Reply