View previous topic :: View next topic |
Author |
Message |
user118696 Apprentice

Joined: 16 Sep 2005 Posts: 276
|
Posted: Tue Jun 19, 2007 12:57 pm Post subject: [HOWTO] Google command line search script (via Links) |
|
|
[HOWTO] Google command line search script (via Links)
I finally managed to come up with a nice little script that lets me search directly Google via the command line. Be warned though : it's not bullet proof. If you enter unusual search characters, it may misbehave. But it's quite stable for common usage. In particular, double quotes are handled pretty well.
I hope this is useful for someone out there
Code: | #!/bin/bash
#
# google.sh
# ---------
# Automatic Google search from the command line.
#
# Syntax : $ google {search terms}
#
if [ -z $1 ]
then
# If no search term --> start Links on the Google page
#
links http://google.ca/
else
#URL construction
#
url='http://google.ca/search?num=50&hl=en&safe=off&q='
appended=0
for searchTerm in "$@"
do
# Replace white spaces in the search terms
#
searchTerm=`echo $searchTerm | sed 's/ /%20/g'`
url="$url%22$searchTerm%22"
if [ $appended -lt `expr $# - 1` ]
then
url="$url"\+
else
url="$url"\&btnG\=Google\+Search\&meta\=
fi
let "appended+=1"
done
links $url
fi
exit 0 |
ORIGINAL POST :
I'm trying to write a very simple script which will start Links with a Google search automatically.
I haven't found the syntax to use after http://www.google.ca/(here)
I want the search terms to come from the command line like this:
Code: | $ google planet earth "atlantic ocean" |
This should search for planet earth "atlantic ocean" on Google.
The script for now:
Code: | #!/bin/bash
#
# google.sh (FYI : it is executable)
#
# Type google {search terms} at the command line. (FYI : There is a symbolic link named
# 'google' that points to google.sh)
#
links http://www.google.ca/??????????(with $* in it)?????????? |
Question - What's the syntax after google.ca/ for a direct search?
Thanks.
Last edited by user118696 on Mon Jun 25, 2007 11:34 pm; edited 1 time in total |
|
Back to top |
|
 |
trolley Apprentice


Joined: 12 Jun 2002 Posts: 292 Location: Canada
|
Posted: Tue Jun 19, 2007 1:34 pm Post subject: |
|
|
I'd try:
Quote: | http://www.google.ca/search?hl=en&q=$1+$2+$3&btnG=Google+Search&meta= |
etc. Of course you should use a loop, start with http://www.google.ca/search?hl=en&q= and keep appending the $1, $2, etc. for each parameter. This is just a wild guess based on actually trying a search via the Google site. |
|
Back to top |
|
 |
user118696 Apprentice

Joined: 16 Sep 2005 Posts: 276
|
Posted: Fri Jun 22, 2007 2:23 am Post subject: |
|
|
trolley wrote: | Code: | http://www.google.ca/search?hl=en&q=$1+$2+$3&btnG=Google+Search&meta= |
|
Tried this already.
Doesn't work.
Links gets started (as shown by ps -A) but it does not appear on screen (which blanks). It stays in the background even after Ctrl-C is hit.
What is missing? I guess it's a quite stupid bash scripting mistake. |
|
Back to top |
|
 |
SiberianSniper Guru


Joined: 06 Apr 2006 Posts: 381 Location: Dayton, OH, USA
|
Posted: Fri Jun 22, 2007 3:18 am Post subject: |
|
|
I think you need to escape the ampersands, ex:
http://www.google.ca/search?hl=en\&q=$1+$2+$3\&btnG=Google+Search\&meta=
if that doesn't work, try replacing links with echo in the bash command for debugging? |
|
Back to top |
|
 |
user118696 Apprentice

Joined: 16 Sep 2005 Posts: 276
|
Posted: Fri Jun 22, 2007 10:59 am Post subject: |
|
|
SiberianSniper wrote: | I think you need to escape the ampersands, ex:
http://www.google.ca/search?hl=en\&q=$1+$2+$3\&btnG=Google+Search\&meta=
if that doesn't work, try replacing links with echo in the bash command for debugging? |
Of course... :( That was a stupid scripting error. It works now, partly though.
Question : Does anyone know how to tell 'my script' to take everything on the command line after google? I mean this especially for the quotes (' or "). They are not taken into account. I'd like them to be part of $* as every other character. |
|
Back to top |
|
 |
JeliJami Veteran


Joined: 17 Jan 2006 Posts: 1086 Location: Belgium
|
Posted: Fri Jun 22, 2007 12:12 pm Post subject: |
|
|
From the Advanced Bash-Scripting Guide:
Quote: |
$*
All of the positional parameters, seen as a single word
"$*" must be quoted.
$@
Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without
interpretation or expansion. This means, among other things, that each parameter in the argument list
is seen as a separate word.
Of course, "$@" should be quoted.
|
_________________ Unanswered Post Initiative | Search | FAQ
Former username: davjel |
|
Back to top |
|
 |
user118696 Apprentice

Joined: 16 Sep 2005 Posts: 276
|
Posted: Mon Jun 25, 2007 11:36 pm Post subject: |
|
|
I'm done whith the script I intended to build. Everything works just fine for me. Have a look at the first post of this topic for an updated script. Any improvement is welcome, of course. |
|
Back to top |
|
 |
mark_alec Bodhisattva


Joined: 11 Sep 2004 Posts: 6066 Location: Melbourne, Australia
|
Posted: Tue Jun 26, 2007 9:07 am Post subject: |
|
|
Moved from Portage & Programming to Documentation, Tips & Tricks. _________________ www.gentoo.org.au || #gentoo-au |
|
Back to top |
|
 |
|