views:

253

answers:

3

I have the following code which I call google

#!/bin/bash

q=$1
open "http://www.google.com/search?q=$q"

It opens Firefox with the keyword. For example, by

google cabal

I want to have specific keywoards added to the command when I put a parameter after the command. The following is an example

google -x cabal

It searches the sequence, for instance

"cabal is"

How can you add a parameter to your command in Bash?

+3  A: 
#!/bin/bash
while getopts "x:" option; do
  case "$option" in
    x) keyword="$OPTARG";;
  esac
done
#echo "$keyword"
open "http://www.google.com/search?q=$keyword"

The : specifies that after x is an argument expected.

seb
Where do I need to put the open -command in your script?
Masi
I added the open command to it.
seb
I am not sure about the $OPTARG parameter. I want to add only the word "is" to the parameter $1. Should I put it to the parameter $option and then call it in the open statement?
Masi
add it for example at x) keyword="$OPTARG is";;
seb
@seb: the command seems to have a bug. When, I do not use the option x, it puts me to the site: http://www.google.com/webhp
Masi
+2  A: 
#!/usr/bin/env bash

while [[ $1 = - ]]; do
    case $1 in
        -x) shift; query+=" $1 is"     ;;
        -d) shift; query+=" define:$1" ;;
        -s) shift; query+=" site:$1"   ;;
        -t) shift; query+=" title:$1"   ;;
        -i) params+="&btnI"            ;;
        # ...
        -h)
            echo "usage: ${0##*/} [-x arg] [-d arg] [-s arg] [-t arg] [-ih]"
            echo
            echo "    -x: Add '[arg] is' to the google query."
            echo "    -d: Add 'define:[arg]' to the google query."
            echo "    -s: Add 'site:[arg]' to the google query."
            echo "    -t: Add 'title:[arg]' to the google query."
            echo "    -i: Do an I'm Feeling Lucky-search."
            echo "    -h: Show this help text."
            exit ;;
    esac
    shift
done

query+="$*" # implode all other arguments into the query string.

open "http://www.google.com/search?q=$query$params"
lhunath
+3  A: 

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.

paxdiablo
@Pax: Thank you for your answer! -- I had a small problem with searches of many words. The problem can be solved by putting the search pattern in quotes.
Masi