tags:

views:

194

answers:

2

My current code

#!/bin/bash

#w=$2
#e=$3
#r=$4
#t=$5
#o=$5
#
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;;
    -wo)
        prepend=""
        append=" site:.mathworld.wolfram.com/"
        shift;;
    -so)
        prepend=""
        append=" site:.stackoverflow.com/"
        shift;;
    -b)
        prepend=""
        append=""
        shift;;



esac
if [[ -z "$1" ]] ; then      # I am not sure about this line and about -z 
    query=""
else
    query="?q=${prepend}${1}${append}${params}"
fi
# you can use echo to put the url to history, eg
# echo http://www.google.com/search${query}

open "http://www.google.com/search${query}"

I want an if-else loop for the open command such that

My unsuccessful attempt

if $1==-b; then$
     open "http://www.books.google.com/search${query}"
 else
     open "http://www.google.com/search${query}"                               
 fi

I get by executing the shell script with gg

gg -b "Freakonomics"        
line 54: Freakonomics==-b: command not found

How can you check the value of $1 such that you can start a google-books-search in Firefox?

+5  A: 

try:

if [ "$1" = "-b" ]; then
    open "http://www.books.google.com/search${query}"
else
    open "http://www.google.com/search${query}"                               
fi

Edit: Added spaces before and after the comparison operator, thanks to Dennis Williamson's comment.

eleven81
You need spaces around that equal sign or it'll always evaluate as true. Unless you need compatibility with sh, you should always use double brackets in bash: if [[ "$1" == "-b" ]]; then
Dennis Williamson
The code is now the following: http://dpaste.com/59761/ . I run % gg -b "Freakonomics". I get a default google search. --- Do you know what can be the reason?
Masi
+2  A: 

eleven81 has it right. Take a look at the Advanced Bash Scripting Guide for more info on comparisons.

Seth Johnson