Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: How can I check the return value of a command

I am new to bash scripting and want to write a short script, that checks if a certain program is running. If it runs, the script should bring the window to the foreground, if it does not run, the script should start it.

#!/bin/bash

if [ "$(wmctrl -l | grep Wunderlist)" = ""]; then
    /opt/google/chrome/google-chrome --profile-directory=Default --app-id=ojcflmmmcfpacggndoaaflkmcoblhnbh
else
    wmctrl -a Wunderlist
fi

My comparison is wrong, but I am not even sure what I should google to find a solution. My idea is, that the "$(wmctrl -l | grep Wunderlist)" will return an empty string, if the window does not exist. I get this error when I run the script:

~/bin » sh handle_wunderlist.sh                                       
handle_wunderlist.sh: 3: [: =: argument expected
like image 367
Natjo Avatar asked Sep 10 '25 04:09

Natjo


1 Answers

You need a space before the closing argument, ], of the [ (test) command:

if [ "$(wmctrl -l | grep Wunderlist)" = "" ]; then
    ....
else
    ....
fi

As a side note, you have used the shebang as bash but running the script using sh (presumably dash, from the error message).

like image 150
heemayl Avatar answered Sep 13 '25 16:09

heemayl