hjf

Monday, December 28, 2015

Web search query from command line (Windows and Linux)

Web search query from command line (Windows and Linux)



If you are like me and you do as much as you can on a computer by command line then there may have been a time in your life that you wanted to do your web searches straight from the command line (cmd/terminals etc...). Also, if you are like me, then you like to use as few 3rd party apps as possible and scripts only if possible.

In Linux there are text based browsers for command line and in Windows you can use Telnet or Links, but like I said, I like scripts (or exe/bin conversions of) and for years I have made sloppy variations of search engine scripts for both OS's. Recently I have perfected and cleaned up those scripts for general use for the public.

Each script has comments/remarks explaining each section and the Windows bat/cmd script has been made easy to use in batch to exe conversions and still work normally if not. Both scripts (Windows/Linux) use the same simple syntax by passing your query directly to the file; which, the files can be named whatever you want. For example if I wanted to use the script as a google search tool and named the script google.cmd(or bat) or google.sh (no extension necessary if shell is stated in file) then I would use any of these syntaxes to search your query:
Windows:

google my search terms
google.cmd my search terms
google.bat my search terms
Linux:
google my search terms
google.sh my search terms
sh google.sh my search terms
Of course you can name your file to whatever you want. If no search terms are provided it just opens the search engines web page.

Script Codes:
For each script you can change 3 variables:

  • Search Engine Url - all the way to right before the search string. E.g. https://www.google.com/#q=

  • Web Browser - your preference, Chrome, Firefox, IE, Opera etc... E.g. C:\Program Files\Google\chrome.exe

  • Spaces delimeter - urls don't work well with spaces so most spaces are replaced by %20, +, or similar.

Windows batch or cmd. Can be converted to an exe with a bat2exe program. Comments/Remarks denoted by 'REM'. For batch files with goto :EOF to work properly remember to leave an empty line at the end of the script file:
@echo off
setlocal EnableDelayedExpansion
REM Command line search utility;
REM can be used for any search engine.
REM by Lateralus138 aka Ian Pride

REM Variables; change as needed for any url search engine;
REM surl = Main search url;
REM bexe = Browser executable; The name or direct link to 
REM ...... the web browser you want to use;
REM spac = Space delimeter expansion/replacer. Most
REM ...... search engines use %20 (%%20 for this script),
REM ...... but some use the + sign;
set "surl=https://www.google.com/#q="
set "bexe=chrome.exe"
set "spac=+"


REM Check if parameters are passed; if not, open regular site;
REM If parameters are passed then continue with processing
REM search terms below;
if "%~1"=="" (
 echo Parameter/s not given.
 start "Starting Search Engine..." "%bexe%" "%surl%"
 goto :EOF
)

REM Process search query terms if given and open in a browser;
REM Variables; Do not change;
REM qry_prms = search terms passed to this script;
REM clr_file = Calling file. Processed out in variable expansion
REM .......... for use in bat2exe programs;
set "qry_prms=%*"
set "clr_file=%0 "
REM Delayed expansion to manipulate search query string for
REM use in your web browser;


REM Replace calling file for bat2exe type programs;
REM qry_strng = Extract calling file from the
REM ........... query parameters;
set "qry_strng=!qry_prms:%clr_file%=!"

REM Replace spaces with delimeter;
set qry_fnl=!qry_strng: =%spac%!

REM Process search query; open browser and searches;
start "Searching For Given Query..." "%bexe%" "%surl%!qry_fnl!"

Linux script or bin. It was, of course, easier to write this than the Windows one. Comments/Remarks denoted by '#':
#!/bin/bash
# Command line search utility;
# can be used for any search engine.
# by Lateralus138 aka Ian Pride

# Variables; change to suit your needs:

# Whatever internet browser you want to use
brwsr_clnt="google-chrome"

# Main search url all the way up to right 
# before the search string.
srch_url="http://kat.cr/usearch/"

# Replacement character for space character in
# the browsers search string url. Most use %20
# but some use the + character or similar.
brwsr_dlmtr="%20"

# Variables; End.

# Main script;

# Test if search string is given, if not, open
# the normal search site without a query in the
# browser of your choice. Exits script.
if [ -z "$*" ]; then
 "$brwsr_clnt" "$srch_url" &
 exit
fi

# If search string given then process string
# and then opens the query in the browser
# of your choice.
qry_strng="$*"
"$brwsr_clnt" "$srch_url${qry_strng// /$brwsr_dlmtr}"

# Main script; End.
Please feel free to comment to or message me with any questions.

No comments: