Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Search sites via Google and Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Search sites via Google and Terminal
Authored by: johnga1t on Jun 11, '07 08:31:03AM
agreed that passing arguments (as opposed to waiting for stdin) is the way to go. this version supports quoting and has an extra for a "feelin lucky" switch, so that
 goog -l "this that" and the "other thing" 
will send you straight to the page for the first hit.

really getopts should be used with a switch to specify a particular site to search.

#!/bin/bash

google="http://www.google.com/search?"

if [ $# -lt 1 ]
    then
    scriptname=`basename $0`
    echo ""
    echo "usage: $scriptname [-l] keyword1 [keyword2 [\"key word 3\"]]" >&2
    echo ""
    exit 1
fi

# if the argument -l is passed, use the "feelin lucky" feature
# and go to first hit
if [ "$1" == "-l" ]
    then
    google=$(echo $google"btnI=I%27m+Feeling+Lucky&")

    shift
fi
google=$(echo $google"q=")


for i 
do
  # get number of words in argument
  numwords=`echo "$i" | wc -w`
  if [ $numwords -gt 1 ];
      then
      # more that one word -> quote and replace spaces w/ +
      arg=%22${i// /+}%22
  else
      arg=$i
  fi
  # add to search string
  google=$google$arg+
done
# remove trailing +
google=`echo -n $google | sed -e 's/\+$//'`

# open browser w/ search link
open "$google"
all that being said, i agree with the above that there are better ways to do these things. especially with firefox keyword searches.

[ Reply to This | # ]