-1

Heyo guys. I am using bash script to get current location of my mouse, but I stuck with this. when I do:

xdotool getmouselocation
x:688 y:411 screen:0 window:98568199

I got my output as a string, I am kinda newbie. How can I get values of x and y into some variables, so I can use them further. Thank you.

ChynaJake
  • 51
  • 1
  • 6
  • 1
    You made _some_ effort in trying to get your requirement done? Even if its trivial it should be fine – Inian Nov 30 '17 at 13:55

1 Answers1

1

The x coordinate is the first word in the output (taking for granted that the space is the word separator). y coordinate is the second one. So:

#!/bin/bash
#
output=$(xdotool getmouselocation)
x=$(echo $output | awk '{print $1}' | cut -d":" -f2)
y=$(echo $output | awk '{print $2}' | cut -d":" -f2)

echo "X= $x"
echo "Y= $y"

The awk prints the word you asked for ($1 or $2) and cut gives you what follows the ':' character.

Nic3500
  • 8,144
  • 10
  • 29
  • 40