Mine the Harvest

Brilliant Bash Profile Functions

by on May.22, 2009, under Linux

Whilst perusing about, I chanced upon these great bash functions (original author unknown) which you and place in your .bashrc  They are a great example of how simple it can be to define a function in Bash and are actually pretty useful. First some examples of the output:

paracelsus@Callandor:~> weather London
Weather for London, UK 59°F Current: Cloudy Wind: W at 9 mph Humidity: 67%

paracelsus@Callandor:~> translate cat japanese
“cat” in Japanese:              猫

paracelsus@Callandor:~> define haxor
* Leet or Eleet (sometimes rendered l33t, 1337, or 31337), also known
* A variant spelling of hacker in the leet argot

Note that as a defined function auto completion of the initial call works too, which I didn’t know.

The code to implement this is quite simple really and is the same as the site above, only I changed one line to update a reference so the translation function works:

(Displayed in the nifty new wp-codbox plugin I just installed.)

# richs-lxh needs to find rude words in other languages, define new groovy  words that the kids use nowadays, and see if it's sunny ; D

# Weather by placename # weather "Barcelona"
weather ()
{
declare -a WEATHERARRAY
WEATHERARRAY=( `lynx -dump "http://www.google.com/search?hl=en&lr=&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=weather+${1}&btnG=Search" | grep -A 5 -m 1 "Weather for" | grep -v "Add to "`)
echo ${WEATHERARRAY[@]}
}

#Translate Spanish/English  - USAGE: translate lamer spanish  # See dictionary.com for available languages (there are many).
translate ()
{
TRANSLATED=`lynx -dump "http://translate.reference.com/browse/${1}" | grep -i -m 1 -w "${2}:" | sed 's/^[ \t]*//;s/[ \t]*$//'`
if [[ ${#TRANSLATED} != 0 ]] ;then
   echo "\"${1}\" in ${TRANSLATED}"
   else
   echo "Sorry, I can not translate \"${1}\" to ${2}"
fi
}

# Define a groovy word - USAGE: define lamer
define ()
{
lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 3 -w "*"  | sed 's/;/ -/g' | cut -d- -f1 > /tmp/templookup.txt
         if [[ -s  /tmp/templookup.txt ]] ;then
            until ! read response
               do
               echo "${response}"
               done < /tmp/templookup.txt
            else
               echo "Sorry $USER, I can't find the term \"${1} \""
         fi
rm -f /tmp/templookup.txt
}

These function templates are great and provide examples from which all manner of interesting things could be made. Thanks to whomever originally contributed them!

Cheers,
Pete

5 comments for this entry:
  1. Kyle

    Truly, the command line is the most powerful thing. But what happened to a picture is worth a thousand words? Maybe the shell is mightier than the sword?

  2. Max

    The command line is a truly powerful thing. But not as powerful as a good programming language. However binary in general, in this era, is more powerful than the most explosive bomb.

  3. admin

    You guys are both right – the shell is mightier than the sword! And while higher (and lower) languages are more powerful, it is hard to beat the impressive things you can do with the simple shell we use every day. Its like discovering your every day car can fly, make coffee and send mothers day flowers automatically – and you only ever thought it could really just carry you around. Bash, you are my hero.

  4. crouse

    I am the original author of these bash functions 😉
    You can find those and alot more at bashscripts.org

    http://bashscripts.org/forum/viewtopic.php?f=28&t=346&p=1217&hilit=cool+functions#p1217

    Nice blog 🙂

  5. admin

    Hi there!

    Thanks for stopping by, I appreciate your comments! Great to get some input from “the guy” himself! I really liked these functions, both for what they do and as a learning tool. Thanks for sharing them with everyone, and I hope that others might find them there here or at http://bbs.archlinux.org/viewtopic.php?pid=416977 or at bashscripts.org

    It’s always nice to meet the creator of something useful.

    Cheers,
    Pete

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...