I'm trying to write a script which will check if gnome-terminal is already open, switch to it if it is, or open it if it doesn't.
# check if terminal is running
EXISTS=$(pgrep gnome-terminal)
# if exists is a number
if [[ $EXISTS =~ ^[0-9]+$ ]]; then
# switch to it
wmctrl -a gnome-terminal
echo "Switched to terminal"
else
# else start it
gnome-terminal
echo "Started terminal"
fi
Nothing happens when I run this. It's difficult to test because the terminal is always open if I'm in there trying to do things.
Any ideas what I'm doing wrong? or is there a better way to go about this?