0

I can't find a command to print on a browser a string in a shell script. For example here on stackoverflow. I'd like to create a script that writes a string in the searchbar and search

xdotool mousemove x y # position of the searchbar
xdotool mouseclick 1 # leftclick
[[command that writes a string on the searchbar]]
xdotool key KP_Enter # press enter

It should be easy but I can't find it

gepqo
  • 7
  • 1
  • 1
    You can't simply write text on top of where the search bar is and expect it to be passed to the browser; you need a tool that can interact programmatically with the browser, or at least with the window manager. `bash` is neither. – chepner May 17 '17 at 19:42
  • @chepner there's a workaround of sorts that accomplishes most (if not all) of what is asked on the question. Check my answer (comments welcome). – Jamil Said May 17 '17 at 21:08

2 Answers2

0

There might be an approximate way to do what you want (tested in Linux Debian 8). You could -- as a normal user, not root (see this for the reason) -- run the following command (on a terminal):

firefox "http://stackoverflow.com/search?q=my_search_term"

That would open firefox on a Stack Overflow's page that would display the search results for the term my_search_term.

If you need this code to be run on a script that runs as root, it would still be possible to (safely) run this command if you run it as a "normal" user instead of root, such as:

su - myuser -c 'firefox "http://stackoverflow.com/search?q=my_search_term"'

To find out who is the user who should be used to run the command above, one of the best options would be to use the logname command, such as:

myuser="$(logname 2>/dev/null)"

Note: This workaround of sorts would also works on many other web sites, such as Google, just substitute with the right url address, such as:

firefox "http://www.google.com/search?q=my_search_term"
Jamil Said
  • 1,825
  • 3
  • 12
  • 18
-1

Would answer in this question be useful? Just replace url with apropriate search string https://stackoverflow.com/search?q=foobar

Community
  • 1
  • 1
grundic
  • 3,898
  • 3
  • 23
  • 38