domingo, 3 de abril de 2011

Query WikiPedia via console/shell script

Sometimes we need an quick answer about something...yeahh! We have google :-). But if we are working without graphic environment or if we need an quickly quickly answer?

We can make an query from command line to wikipedia and we got the response instantly, without advertisements, without open pages in web browser and without search in web page from the answer that we need.

So I developed an shell script to do this job for us:
#!/bin/bash
# Developed by Antonio Mendes 3/04/2011
#
# about - this script returns a few documentation about something
#

QUERY_STR=$@

TEST_NET=$(ping -c 1 www.wikipedia.org  2> /dev/null | grep "0% packet loss")

if [ "$TEST_NET" != "" ]; then
        ANSWER=$(dig +short txt "$QUERY_STR".wp.dg.cx)
else
        echo "Network down, please try again later!"
        exit 1
fi

if [ "$ANSWER" != "" ];then
        echo $ANSWER
else
        echo "Your query don't return any result, please make sure that it's a valid term!"
fi
Next save as about and give execution permissions.

To execute you must call the script wiht the word to query like this:
 antonio@pc:~$ ./about Benfica
"Sport Lisboa e Benfica (\; commonly referred to as simply Benfica, S.L. Benfica and in many fan anthems as SLB ) is a multisports club based in Lisbon, Portugal, best known for its football team. Sport Lisboa e Benfica was founded in 1904 by a group of twe" "nty-four men. The club's main stadium is the Est\195\161dio da Luz in the Luz area of Lisbon... http://a.vu/w:S.L._Benfica"
 antonio@pc:~$ ./about Linux
"Linux (commonly pronounced in English\; variants exist) is a generic term referring to Unix-like computer operating systems based on the Linux kernel. Their development is one of the most prominent examples of free and open source software collaboration\; t" "ypically all the underlying source code can be used, freely modified, and redistributed by anyone under the terms of the... http://a.vu/w:Linux"
It's all, from now on you have an Wikipedia in your command line.

Sem comentários:

Enviar um comentário