I'm trying to create a bash script that records the location of the mouse every 5 milliseconds. I also want to record the timestamps and locations of mouse-clicks.
Recording the mouse location has been easy with xdotool getmouselocation. I've been able to record mouse clicks using some of the suggestions here: https://unix.stackexchange.com/questions/106736/detect-if-mouse-button-is-pressed-then-invoke-a-script-or-command
However, I've not been able to combine the two.
Is there any way to accomplish this? Thank you in advance!
In accepted answer of https://unix.stackexchange.com/questions/106736/detect-if-mouse-button-is-pressed-then-invoke-a-script-or-command have an example to get mouse status change. With little modification you get print the mouse location upon mouse button down.
#Gem Taylor mentioned using script language for this is not an optional way.
During test run, I experienced cases when clicks are not get captured.
#!/bin/bash
MOUSE_ID=$(xinput --list | grep -i -m 1 'mouse' | grep -o 'id=[0-9]\+' | grep -o '[0-9]\+')
STATE1=$(xinput --query-state $MOUSE_ID | grep 'button\['"."'\]=down' | sort)
while true; do
sleep 0.005
STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\['"."'\]=down' | sort)
CLICK=$(comm -13 <(echo "$STATE1") <(echo "$STATE2"))
if [[ -n $CLICK ]]; then
echo "$CLICK"
xinput --query-state $MOUSE_ID | grep 'valuator\['
fi
STATE1=$STATE2
done
Related
I've got a long running process and I want to capture a tiny bit of data from the big swath of output.
I can do this by piping it through grep, but then I can't watch it spew out all the other info.
I basically want grep to save what it finds into a variable and leave stdout alone. How can I do that?
With tee, process substitution, and I/O redirection:
{ var=$(cmd | tee >(grep regexp) >&3); } 3>&1
There isn't any way to use a variable like that. It sounds like you want to write your own filter:
./some_long_program | tee >(my_do_stuff.sh)
Then, my_do_stuff.sh is:
#!/bin/bash
while read line; do
echo "$line" | grep -q 'pattern' || continue
VARIABLE=$line # Now it's in a variable
done
If you have the storage space, this is probably more like what you want:
./some_long_program | tee /tmp/slp.log
Then, simply:
grep 'pattern' /tmp/slp.log && VARIABLE=true
or:
VARIABLE=$(grep 'pattern' /tmp/slp.log)
This will let you run the grep at any time. I don't think the variable really adds anything though.
EDIT:
#mpen Based on your last answer above, it sounds like you want to use xargs. Try:
(echo 1 ; sleep 5 ; echo 2) | xargs -L1 echo got:
The -L1 will run the command for every instance found, otherwise it grabs lots of stdin and passes them all (up to some maximum) to the command at once. You'll still want to use this via tee if you want to see all the command output as well:
./some_long_program | tee >(grep 'pattern' | xargs -L1 ./my_do_stuff.sh)
I think this can be done a little more simply if we tee to /dev/tty:
❯ BAR=$(echo foobar | tee /dev/tty | grep -Pom1 b..); echo "[$BAR]"
foobar
[bar]
I'm trying to build a script that checks that windows under XFCE are minimized before displaying a window I've chosen (it's a part from a bigger project)
I tried to recover the count of the open windows with wmctrl
but these are not the minimized windows :
CURRWORKSPACE=$(wmctrl -d | grep '*' | cut -d ' ' -f1)
OPENWINDOWS=$(wmctrl -l | cut -d ' ' -f3 | grep $CURRWORKSPACE | wc -l)
I also try with xdotool, but without success :(
I was wondering if you knew of any way to get this information.
I'm on XFCE, but another way with any tool would be great
Many thanks !
Given a window and its id as listed by wmctrl you can use the following function to determine whether that window is minimized. Note that minimized windows are called iconic in X.
# usage: isMinimized <windowId>
# returns status 0 if and only if window with given id is minimized
isMinimized() {
xprop -id "$1" | grep -Fq 'window state: Iconic'
}
In order to count open windows you can loop over the list of window ids.
openWindows() {
count=0
for id in $(wmctrl -l | cut -f1 -d' '); do
isMinimized "$id" || ((count++))
done
echo $count
}
At least on my desktop environment (Cinnamon) some "windows" were always open. These windows are for instance the Desktop. I adjusted the function by filtering out these windows before looping over them. Since they were sticky and I normally don't use sticky windows I just neglected all sticky windows: $(wmctrl -l | grep -vE '^0x\w* -1' | cut -f1 -d' ').
You can adjust the filtering to your needs. In this case all open and non-sticky windows on all workspaces/desktops are counted.
I have created and script that uses xdotool to open new tabs in specific firefox containers. I need to map three keys, for instance ctrl+y+p to open a personal container, ctrl+y+b to open the banking container and so on. I cannot find a tool that lets you map more than two keys.
Is there something like what I have described?
Thanks a lot.
Edit: For anyone looking for an answer, I found a way. I use i3 so I just added a mode triggered by pressing the first two keys and then mapped the "b" to the command + escape (to exit the mode).
Install xdotool if you haven't already done so, for example:
sudo apt-get install xdotool
As per your example (CTRL+y+p), execute:
DISPLAY=:0 xdotool keydown ctrl keydown y keydown p
Which presses the three keys but does not release them after. An alternative way:
DISPLAY=:0 xdotool key ctrl+y+p
:0 is a typical value for the display, but in my Raspberry Pi, for example, it needed to be DISPLAY=:10. If you are still having trouble with the display variable and get a message like this:
xdotool can't open display (null)
then you could obtain the display value with:
ps -u $(id -u) -o pid= | \
while read pid; do
cat /proc/$pid/environ 2>/dev/null | tr '\0' '\n' | grep '^DISPLAY=:'
done | grep -o ':[0-9]*' | sort -u
Another alternative way to obtain DISPLAY values (it will be the number after the ':' )
xauth list
This works in raspbian too:
w -hs | awk '{print $3}' | sort -u | grep -v -
Good luck
I have a page exported from a wiki and I would like to find all the links on that page using bash. All the links on that page are in the form [wiki:<page_name>]. I have a script that does:
...
# First search for the links to the pages
search=`grep '\[wiki:' pages/*`
# Check is our search turned up anything
if [ -n "$search" ]; then
# Now, we want to cut out the page name and find unique listings
uniquePages=`echo "$search" | cut -d'[' -f 2 | cut -d']' -f 1 | cut -d':' -f2 | cut -d' ' -f 1 | sort -u`
....
However, when presented with a grep result with multiple [wiki: text in it, it only pulls the last one and not any others. For example if $search is:
Before starting the configuration, all the required libraries must be installed to be detected by Cmake. If you have missed this step, see the [wiki:CT/Checklist/Libraries "Libr By pressing [t] you can switch to advanced mode screen with more details. The 5 pages are available [wiki:CT/Checklist/Cmake/advanced_mode here]. To obtain information about ea - '''Installation of Cantera''': If Cantera has not been correctly installed or if you do not have sourced the setup file '''~/setup_cantera''' you should receive the following message. Refer to the [wiki:CT/FormulationCantera "Cantera installation"] page to fix this problem. You can set the Cantera options to OFF if you plan to use built-in transport, thermodynamics and chemistry.
then it only returns CT/FormulationCantera and it doesn't give me any of the other links. I know this is due to using cut so I need a replacement for the $uniquepages line.
Does anybody have any suggestions in bash? It can use sed or perl if needed, but I'm hoping for a one-liner to extract a list of page names if at all possible.
egrep -o '\[wiki:[^]]*]' pages/* | sed 's/\[wiki://;s/]//' | sort -u
upd. to remove all after space without cut
egrep -o '\[wiki:[^]]*]' pages/* | sed 's/\[wiki://;s/]//;s/ .*//' | sort -u
I have a Linux driver running in the background that is able to return the current system data/stats. I view the data by running a console utility (let's call it dump-data) in a console. All data is dumped every time I run dump-data. The output of the utility is like below
Output:
- A=reading1
- B=reading2
- C=reading3
- D=reading4
- E=reading5
...
- variableX=readingX
...
The list of readings returned by the utility can be really long. Depending on the scenario, certain readings would be useful while everything else would be useless.
I need a way to grep only the useful readings whose names might have have nothing in common (via a bash script). I.e. Sometimes I'll need to collect A,D,E; and other times I'll need C,D,E.
I'm attempting to graph the readings over time to look for trends, so I can't run something like this:
# forgive my pseudocode
Loop
dump-data | grep A
dump-data | grep D
dump-data | grep E
End Loop
to collect A,D,E as that would actually give me readings from 3 separate calls of dump-data as that would not be accurate.
If you want to save all result of grep in the same file, you can just join all expressions in one:
grep -E 'expr1|expr2|expr3'
But if you want to have results (for expr1, expr2 and expr3) in separate files, things are getting more interesting.
You can do this using tee >(command).
For example, here I process the same pipe with thre different commands:
$ echo abc | tee >(sed s/a/_a_/ > file1) | tee >(sed s/b/_b_/ > file2) | sed s/c/_c_/ > file3
$ grep "" file[123]
file1:_a_bc
file2:a_b_c
file3:ab_c_
But the command seems to be too complex.
I would better save dump-data results to a file and then grep it.
TEMP=$(mktemp /tmp/dump-data-XXXXXXXX)
dump-data > ${TEMP}
grep A ${TEMP}
grep B ${TEMP}
grep C ${TEMP}
You can use dump-data | grep -E "A|D|E". Note the -E option of grep. Alternatively you could use egrep without the -E option.
you can simply use:
dump-data | grep -E 'A|D|E'
awk '/MY PATTERN/{print > "matches-"FILENAME;}' myfile{1,3}
thx Guru at Stack Exchange