Since you have two sources of the information (the search term and the modifier), I would use the following. It allows for a single modifier (-x
for appending " is" and enclosing the whole thing in quotes, -d
for prefixing "define:" and enclosing the whole thing in quotes, and -w
for simply adding the search term to limit you to wikipedia).
Note that the placement of quotes is controlled by the modifier since it may need to quote the argument passed to Google or add search terms outside that argument. You have full control over what's produced in the URL (make sure you turn the echo
back into an open
before shipping to production).
#!/bin/bash
prepend=""
append=""
case "$1" in
-h)
echo 'Usage: google [-{hxdw}] [<arg>]'
echo ' -h: show help.'
echo ' -x: search for "<arg> is"'
echo ' -d: search for "define:<arg>"'
echo ' -w: search for <arg> site:wikipedia.org'
exit;;
-x)
prepend="\""
append=" is\""
shift;;
-d)
prepend="\"define:"
append="\""
shift;;
-w)
prepend=""
append=" site:.wikipedia.org"
shift;;
esac
if [[ -z "$1" ]] ; then
query=""
else
query="?q=${prepend}${1}${append}"
fi
echo http://www.google.com/search${query}
And here's some sample output:
pax> google -w "\"bubble sort\""
http://www.google.com/search?q="bubble sort" site:.wikipedia.org
pax> google cabal
http://www.google.com/search?q=cabal
pax> google
http://www.google.com/search
pax> google -d cabal
http://www.google.com/search?q="define:cabal"
pax> google -x wiki
http://www.google.com/search?q="wiki is"
pax> google -h wiki
Usage: google [-{hxdw}] [<arg>]
-h: show help.
-x: search for "<arg> is"
-d: search for "define:<arg>"
-w: search for <arg> site:wikipedia.org
If you don't provide a search term, you'll just get the Google search page.