Results 1 to 4 of 4

Thread: Bash Script for searching google

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question Bash Script for searching google

    Hi all,
    i have a bash script which works almost as i would hope.
    the function is to read a file (Chemical names) line by line,
    and open a firefox tab & do a google search using the Im feeling lucky parameter. This Script is to get the MSDS for each chemical.
    Ah, so it went ok until I had a space in the line. Ethanol, Hexane, water..
    all single line stuff works fine. But Oxalic acid would search generate 2 incorrect searches. So I know my while loop is screwy. But, I'm not sure how to fix it. Its a pretty simple script, can some one please help?

    Code:
    #!/bin/bash 
    x="http://www.google.com/search?q="  
    L="&btnI=Im+Feeling+Lucky"
    y="&as_filetype=pdf"
    z=0
    while read line
    do z=$(($z+1));
    firefox $x$line$y$L;
    #sleep so google doesn't baulk about suspicious traffic
    sleep 3
    done < "NeededMSDS.txt"

    The fix I came up with was to insert "%20" into every place there was a space in the Chemical Names list. Preferably however, I would like to not alter my data set.
    ~Conradin~

  2. #2
    Join Date
    Jun 2005
    Location
    Toronto, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Bash Script for searching google

    In place of:
    firefox $x$line$y$L;
    Try:
    Code:
    firefox $x`echo "$line" | sed -e 's/ /\%20/g'`$y$L

  3. #3
    Join Date
    Apr 2008
    Location
    Far, far away
    Beans
    2,148
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bash Script for searching google

    Use parameter substitution in bash.

    Replace

    firefox $x$line$y$L;

    with

    firefox $x${line// /%20}$y$L;

    This takes $line but replaces " " with "%20". The extra "/" in the pattern makes it do all occurences not just the first.

  4. #4
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash Script for searching google

    XD BkkBonanza !!! Aw sweet! its working perfect now! Thank You SOO much!!

    Quote Originally Posted by BkkBonanza View Post
    Use parameter substitution in bash.

    Replace

    firefox $x$line$y$L;

    with

    firefox $x${line// /%20}$y$L;

    This takes $line but replaces " " with "%20". The extra "/" in the pattern makes it do all occurences not just the first.
    ~Conradin~

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •