Results 1 to 8 of 8

Thread: open the results of a find command in a browser

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    712
    Distro
    Ubuntu 14.04 Trusty Tahr

    Question open the results of a find command in a browser

    Hi Guys,

    OK - that is a pretty bad title for a post.
    I don't really have the proper vocabulary to explain what I mean, but here it goes.

    I have all of the man files on my system converted to html files in a directory in my home.

    If I run:
    Code:
    #!/bin/bash
    read -p "Searching for: " search_string; find /home/grouchygaijin/man-pages-html -iname "$search_string" 2>/dev/null
    It works.
    The script brings up the path to the html file(s) that matches the search.
    Now, I could just be happy and copy the path to the file then paste it into the browser, but I thought it would be much cooler to be able to have the browser open up the file when I click on it.

    The question is:
    How can I tell the script to let me choose to open the search results (path to the file found) in a browser.

    I really appreciate the help!
    Last edited by GrouchyGaijin; April 17th, 2013 at 07:30 AM. Reason: solved
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,402
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: open the results of a find command in a browser

    xdg-open "$file" to open with program associated with the file type or simply browser_name "$file"

    Code:
    #!/bin/bash
    
    dir="$HOME/man-pages-html"
    read -p "Searching for: " search_string
    readarray -t results < <( find "${dir}" -iname "*${search_string}*.html" )
    
    if (( ${#results[@]} == 1 ))
    then 
      xdg-open "${results[@]}"   # open the only result immediately
    elif (( ${#results[@]} > 1 ))
    then
      select s in "${results[@]/#$dir\//}"; do break; done   # allow user to select file from the list of results
      xdg-open "$dir/$s"
    else
      echo "no files found, sorry"
    fi
    Last edited by Vaphell; April 17th, 2013 at 01:17 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    712
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: open the results of a find command in a browser

    Wow!
    THANK YOU!
    Now I need to figure out what you did.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,402
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: open the results of a find command in a browser

    readarray line captures the output of find and stores it in the results array, each line is a separate element, ${results[0]} - 1st element of the array, ${results[1]} = 2nd, etc.
    ${array[@]} = all elements.
    if you want to see the contents of the array you can use something like this:
    Code:
    printf "'%s'\n" "${results[@]}"
    if the array length ( ${#results[@]} ) is 1 then simply run xdg-open <results>

    if it's greater than one: create menu populated with elements of results[] using select. It's a potentially infinite loop in case one wants to put repetitive tasks in it, so break is needed to jump out right after the choice. Whatever you have chosen is opened with xdg-open.
    Array elements store full paths but in menu i trimmed them a bit for aestetic purposes: ${results[@]/#$dir\//} = elements of results[] with $dir path removed. $dir is glued back with selected value in the next line.

    to illustrate:
    Code:
    $ prefix=ABC
    $ x=ABCsomething
    $ echo $x:${x/#$prefix/}
    ABCsomething:something
    $ array=( "ABC111" "ABC222" "ABC333" )
    $ echo ${#array[@]}
    3
    $ printf "'%s'\n" "${array[@]}"
    'ABC111'
    'ABC222'
    'ABC333'
    $ printf "'%s'\n" "${array[@]/#$prefix/}"
    '111'
    '222'
    '333'
    $ select s in "A" "B" "C"; do echo "you selected '$s'"; break; done
    1) A
    2) B
    3) C
    #? 3
    you selected 'C'
    $ select s in "${array[@]/#$prefix/}"; do echo "you selected '$s'"; break; done
    1) 111
    2) 222
    3) 333
    #? 2
    you selected '222'
    Last edited by Vaphell; April 17th, 2013 at 09:35 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: open the results of a find command in a browser

    @OP
    Just wondering why using http://manpages.ubuntu.com or http://manpages.debian.net is not an option?

  6. #6
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    712
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: open the results of a find command in a browser

    Quote Originally Posted by schragge View Post
    @OP
    Just wondering why using http://manpages.ubuntu.com or http://manpages.debian.net is not an option?
    Well, I suppose they are options. They sound like better options actually, I just didn't think of it.
    (I was thinking to myself that I'd probably need to update my directory of man pages if I install a new program.)
    How can I modify this script to use the manpages.ubuntu.com link instead of my local directory.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: open the results of a find command in a browser

    The fourth link under Navigating the Repository on manpages.ubuntu.com is Download the 'dman' shell script to run on a command line. The link before that is Install the search engine plugin for your browser.

    The manpages on manpages.ubuntu.com are just static HTML files, so e.g. dpkg(1) in English for precise is
    Code:
    http://manpages.ubuntu.com/manpages/precise/en/man1/dpkg.1.html
    Last edited by schragge; April 17th, 2013 at 11:03 AM.

  8. #8
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    712
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: open the results of a find command in a browser

    Cool - thanks man!
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

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
  •