Something like this?
Code:
#!/bin/bash
IFS=$'\n'
array=( $( xprop | sed -rn -e '/WM_CLASS/ s/^.*"([^"]*)"$/\1/p' -e '/PID/ s/[^[:digit:]]//gp' ) )
zenity --question --text "Are you sure you want to terminate ${array[1],,}?"
case $? in
0) kill ${array[0]}
exit $?
;;
*) echo "Operation canceled."
exit 3
;;
esac
Note that you can use a single
sed command to do all the work, instead of multiple pipes.
The sed command above seems to reliably output the window name first, then the PID. I chose the last WM_CLASS field though instead of the one you used because, a) it's easier to filter with sed, and b) it seems to give a more reliable name. My browser window outputs a line of
WM_CLASS(STRING) = "Navigator", "Iceweasel", for example.
I then stored the output in an array. Setting IFS to newline ensures that I get one array element per line of output, no matter what the formatting.
Finally we can use a simple exit code test to determine which action to take, since
zenity exits with 0 when "yes" is selected, and 1 when "no" is selected. I also set the script to exit with whatever exit code
kill exits with, or "3" if you select cancel.
There's only one more thing to mention. the two commas in "
${array[1],,}" convert the contents of the variable to lowercase, but this feature is only available in version 4 of bash. Remove them if your shell is an older version and use something like
Code:
array[1]="$(echo ${array[1]} | tr [:upper:] [:lower:] )"
before running the zenity command.