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 &.
Related
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.
This question already has an answer here:
How do I place stdout on edit line?
(1 answer)
Closed 3 years ago.
I run multiple bash script for FFmpeg. However, since I have several windows open, I lose track of what file I ran since I execute the file directly from the script there is no history of what I ran. The only history there is the first file I called
for example:
enter tv number i.e 19: 19
stream key: key
Press [enter] to execute tv19
I press enter, and it runs tv19 but there is no record I ran tv19 in that window, so how can I echo the command to the $
like this
[ibrod ~]$ ./tv19
than I can press enter and use the up arrow than I know what file I ran in that putty window.
Thanks for the help, I figured another way. I change the prompt like [tv10 ~]$
just made a file changetv i put inside
PS1="[$1 \W]\$ "
so I run the file and call it as
. ./changetv tv10
so now I know what window is what. :)
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
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 6 years ago.
I am doing some pentests against one of my websites that is currently being built (a school project)
And I am trying to make sure it's security at it's best.
(Yes, I do have the correct parameters and the site is vulnerable to SQLi Injections.
It does continue it's scan but it will then ask the [y/n] and I choose [y] and it just stops and doesn't scan. I've tried doing a fresh clone of sqlmap and that didn't work.
Anything that can help would be appreciated.
root#kali:~# sqlmap -u http://myschoolproject.com/ --dbs
[1] 1372
bash: --dbs: command not found
(It will scan until asked a [y/n])
it looks like the back-end DBMS is 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] y
[1]+ Stopped sqlmap -u http://myschoolproject.com/
That sounds like you have a & in there. In bash, foo & bar runs the command foo in the background and bar in the foreground.
So if your URL actually looks like http://myschoolproject.com/index.php?cat=4&attr=95,76, that command is interpreted as
sqlmap -u http://myschoolproject.com/index.php?cat=4 &
attr=95,76 --dbs
The first command runs sqlmap in the background (with a truncated URL); this explains the [1] 1372 part (that's what bash shows then starting a background process). The second command runs --dbs in the foreground (with attr set to 95,76 in the environment); this explains the bash: --dbs: command not found error.
In any case, the solution is to quote the URL with single quotes:
sqlmap -u 'http://myschoolproject.com/index.php?cat=4&attr=95,76' --dbs
What I have is an applescript app in xcode that runs a shell script. What I was hoping to do is have the progressIndicator move a certain amount when the command is "echo hello world" as an example but since all of my commands are sudo I have to put them in a shell script together and I can't just have the progressIndicator move in between commands (there are lots of them.) Is there a way to have the bar move when a certain command is started? Also, is there a way to output the log of the applescript to a textView in xcode?
You can always run the command line tool "ps" to see which processes are currently running. As such you can formulate a repeat loop and using ps can figure out which of your commands are currently running... and thus increment your progress indicator as needed.
Here's a ps command I've used to get a nice listing. You can combine this with grep to filter for your processes...
/bin/ps -Axcro user,pid,%cpu,command