It seems that there is a misunderstanding in how the shell's test constructs [ ... ] (or [[ ... ]] in case of Bash) work.
- The
if statement checks if the command following it returns an exit status of 0, which means "no error" and is interpreted as "true". If so, execution switches to the then branch of the script (see the Bash manual e.g.).
- The
pgrep command returns 0 (i.e. "true") if it has found a process matching the pattern. That is why you get running only if an instance of goland.sh is active.
- The
[ is a special command (often a shell builtin) that allows to perform tests on files, strings, and numbers, and will return "true" if the condition enclosed between the opening and closing brackets is true. However, it is designed to check according to a specific syntax if an operation on one or more operands is true, e.g. if one number (operand 1) is greater than (operator) another number (operand 2). E.g., assuming the shell variable n has the value 5, the test
[ "$n" -gt 5 ]
will return true, and when used as the test command of an if statement, the execution will switch to the then branch.
- There are several test constructs defined so that you can check e.g. if a string is empty or not, if a file exists and/or is executable etc. However, in all case you have to understand the correct syntax of the code between the
[ ... ] to ensure that you get the expected result.
Now, you have included a "raw" shell command inside your test construct parentheses. What happens then depends somewhat on the circumstances, but in any case it constitutes a syntax error because you have several (constant) string tokens between the square brackets without a valid operator.
- When using the
[[ ... ]] you will get a syntax error right away because it is a Bash syntax element and the Bash recognizes the wrong syntax.
- In case of the
[ ... ] however your command will fail silently, because [ is actually a command (albeit often builtin) which only happens to expect its arguments to make sense in terms of the pre-defined operators and expects its last argument to be the closing ] - but since you have redirected its error output to /dev/null, the error message will get lost.
- I was not able to reproduce your always getting
running as output with the wrong syntax, in either case.
All in all, you don't need to use the [ ... ] (or [[ ... ]]) constructs because the program you are using for the test already returns "true" if a running process was found. If you wanted to base the test on the output of pgrep, you would need to use a "command substitution" as in $(pgrep ...) (and of course remove the output redirection to /dev/null).
I would recommand checking your shell scripts with shellcheck, also available as standalone program on many Linux distributions, to guard against syntax (and some logical) errors.
if [[ pgrep "goland.sh" >/dev/null 2>&1 ]] ; then ...output an error? Which version of Bash are you using?./check.sh: line 7: conditional binary operator expected[ pgrep "goland.sh" >/dev/null 2>&1 ]should also fail, but silently, using it in anifshould not take thethenbranch