print string on a browser, shell script - bash

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

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"

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

Related

Unable to run xdotool commands from within a bash script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 11 months ago.
I've recently been having some trouble with xdotools and bash scripting. I've dipped my toes into make my Linux install look a little bit nicer, and so I decided to have a bash script open up a window upon login to my DE displaying htop. I have managed to automate the process of opening the window, but I am unable to move the windows because I cannot get the proper window ID as the terminal reports this;
./htop.sh: line 5: search: command not found
Obviously "htop" is the name of the file and "search" is the command I am trying to run to get the window id. Also, to provide some context to this with my code:
#!/bin/bash
# displays 'htop' in the bottom right corner of the screen
xfce4-terminal --hide-borders --hide-toolbar --hide-menubar --title=HTOP --command="bash -c 'clear;htop;$SHELL'" &
WINDOWID=xdotool search --name "HTOP" &
xdotool windowmove $WINDOWID 4526 850 &
Anyways, whenever I run the line,
xdotool search --name "HTOP" # HTOP is the title of the terminal window I open
within my terminal everything works just fine, and as long as the window that the script opens is actually open, it spits out the window ID that I need to further preform the "windowmove" command. So I guess my question is; is this just a bug of xdotool that you cannot preform the functions from withing a bash script? or did I just mess up my syntax somewhere?
WINDOWID=xdotool search --name "HTOP" &
What you are doing here is assigning the string "xdotool" to the variable $WINDOWID.
As the string is followed by a space, your shell interprets everything after the space as a separate command.
If you want to assign the output of a command to a variable you can do that like this:
WINDOWID=&(xdotool search --name "HTOP")
Or by using the deprecated way with backticks:
WINDOWID=`xdotool search --name "HTOP"`
Also note that it makes no sense to run the commands in your script in background (&). Each command relies on the previous to produce a correct result, so what you actually want to do is run them in series, meaning without the trailing &.

Put command string on the bash prompt

Say I have bash prompt in the terminal:
host:~/dir $
how can I write a command to the prompt that the user can choose to run? Maybe there is a way to use readline(3) to put a command in the shell prompt?
In other words, I am looking to write a command here:
host:~/dir $ <write some command here>
I tried:
echo "write some command here" > /dev/stdin
but that didn't quite work - it doesn't seem to put it on the prompt, is there a way to do that?
What I am trying to do - When you hit up/down arrow keys with bash, your previous command shows up in the prompt, I am trying to read from another history file and put it on the prompt.
Without knowing more about what your use case is, I'd start by pointing you in the direction of whiptail. It's part of the base install of most Linux systems and it allows you to present an input box to the user, even allowing for the box to be pre-filled with a default value. A very simple example would look roughly like this:
whiptail --input "Want to run this?" 8 78 "<write command here>" --title "Dialog box title here"
There's a primer available on WikiBooks that does an adequate job of introducing most of its basic features, if you want to dive deeper.

Is there any way to come back to a ready-to-enter command in fish shell by just pressing a combination of keys?

Some times that I have a command ready to press enter but that command I have changed it in some way and it's a long command, then I remember that I have to open a text file (e.g. to get some information that I will use in the command). So what most of the times I do, is to cancel that command (Ctrl+C) and then open the text file get the information I need and then retype the command again with the pasted value from the text file. This is not very efficient for me specially if the server doesn't have any kind of GUI and I can't copy the previous command so I don't lose it.
So my question is, Is there any kind of combination keys that I could use to save a command ready to enter so I don't lose it and I don't have to type it all over again?
Thanks!
This is currently not possible out of the box.
The easiest way to do it is probably to
Change the cancel binding to stash the commandline
Add a binding to recall the stashed commandline
It would work something like this:
The function to recall:
function recall_commandline
if set -q stashed_commandline
commandline -r -- $stashed_commandline
end
end
Add to __fish_cancel_commandline (use funced __fish_cancel_commandline. Once you are happy do funcsave __fish_cancel_commandline):
set -g stashed_commandline $cmd
# right before:
commandline ""
Add to fish_user_key_bindings
bind \cr recall_commandline
This will allow you to press Ctrl+r to recall the last cancelled commandline. Expanding it to multiple is non-trivial (since "commandlines" can have multiple lines), as is adding the commandlines to history so that they can be recalled with the normal bindings.
I have the following function to turn comment/uncomment the current statement:
function toggle-comment-cmd-buffer --description 'Comment/Uncomment the current or every line'
set -l cmdlines (commandline -b)
if test "$cmdlines" = ""
return
end
set -l cmdlines (printf '%s\n' '#'$cmdlines | string replace -r '^##' '')
commandline -r $cmdlines
string match -q '#*' $cmdlines[1]; and commandline -f execute
end
I bind it thusly: bind \e\# toggle-comment-cmd-buffer. This way I can quickly comment and put the current statement in my command history in order to do something else. Then I can recall the comment and press [alt-#] again to remove the comment characters and continue modifying the command.
I set this up in my personal fish config because I had gotten used to doing something similar in ksh93.

Modifying Typed Character Count

I was wondering if it was possible to manipulate the number of characters typed as a command. The reason for this, is I am writing a custom function to complete commands (when you press TAB), but I am using the complete function only for prompting the user, and not completing. What I want to be able to do is complete a typed word as such:
User$ command param[TAB]
prefixparam aaaparambbb
User$ command param
User$ command aparam[TAB]
User$ command aaaparambbb
Since I am completing the param as the prefix and suffix (and in some cases replacing it entirely), I can't use the builtin complete functionality, and instead I am looking for a workaround (hopefully without getting too deep).
How my current function works
When you press tab, it will parse the command and determine the possible completions. Then it will type the bash prompt again ($PS1) and the part of the command that was typed already. (User$ command param)
I want it so that when there is only 1 possible option, for it to replace your partial command with the only remaining option. This is already possible since I am manually reprompting the user, but I am having problems since the shell will not allow you to backspace more than you typed originally.
User$ command aparam[TAB]
User$ command aaapa[rambbb] // Can only backspace 6 characters
I need some way to trick the shell into thinking that all 11 characters were typed and can be deleted.
=========================================================================
I have found almost what I wanted to be possible using bind for anyone interested: bash expand cd with shortcuts like zsh
You can modify the $READLINE_LINE and $READLINE_POINT variables to modify the command. However, this only works for bind commands, and binding to TAB would overwrite and break other autocomplete functions which I would prefer not to.

Escape sequence <ESC>]0;

I am currently trying to write a script that uses expect to logon to SSH. Logging on to a server every prompt appears as [user#host]~/directory$ when I use a xterm color terminal. However, if I read the output from SSH directly with expect I see the following <ESC>]0;user#host:~/directory[user#host]~/directory$. Using export PS1="#-->" changes the result to <ESC>]0;user#host:~/directory#-->.
My question is: What does the sequence <ESC>]0;do? And which class of terminals does it belong to? I could not find it for neither VT52 nor VT100.
by default, the label of each tab is the name of the job that's running in that session. some systems are configured to augment this with additional information such as the hostname you're logged in to or your current directory; this is done by sending a special code of:
ESC]0;<string>^G
such as, ESC]0;david#Scott:~^G, would put "david#Scott:~" in my tab title
this is referred to as the XTERM hardstatus hack.

Resources