1

My script myscript.sh checks if mysqld is running:

mypid=`pgrep mysqld`

if ! (( $mypid > 0 )); then
  echo "no"
else
  echo "yes"
fi

Output (running ./myscript.sh as root):

yes

Now if I change the first line to a process that is not running e.g. mysqld123:

mypid=`pgrep mysqld123`

if ! (( $mypid > 0 )); then
  echo "no"
else
  echo "yes"
fi

Output:

./myscript.sh: line 3: ((: > 0 : syntax error: operand expected (error token is "> 0 ")

no

Expected output: no (without any errors)

So why does changing the process to something that is not running cause a syntax error, how can I resolve it?

1
  • Apparently, pgrep isn't producing any output. Commented Feb 18, 2019 at 12:09

1 Answer 1

2

The logic of this comparison is entirely wrong. You expected if the pid is not available the operator ((..)) would fail and the negation logic would make your result a boolean True. That's not the case happening.

When the pgrep fails to match the process of the given name, it returns an empty result, so your comparison is literally becoming after expanding pgrep mysqld123

! ((   > 0  ))

with the LHS of the > being empty which is a syntax error for the ((..)) as it needs two operands to perform the arithmetic. The ideal logic would have been to use the return code of the pgrep command to the shell when it finds a match or when it fails to find a match as

if ! pgrep mysqld 2>&1 > /dev/null; then
    printf '%s\n' 'msqld not running'
fi

See Arithmetic Expansion in bash for more relevant details on the subject. To answer to OP's question why quoting a variable inside ((..)) wouldn't work? - The ((..)) syntax is an arithmetic operator unlike [ or [[ which can handle empty strings if quoted properly. An empty value doesn't make sense in an arithmetic context, as if asking result of "" + 0

Sign up to request clarification or add additional context in comments.

8 Comments

I see, thank you. Can I check if the result is empty like so: if [ -z "$mypid" ]; then echo "not running" fi or would that be bad? I tried it and it works as far as I can tell
@RandomCoder: You can do that, as long as you quote the variable mypid properly as you've shown. But you can get rid of the use of a variable altogether if you follow my approach
Awesome, thanks. I like the variable because I can use it in logging, like echo "found it running, PID is $mypid"
@RandomCoder: The (( )) syntax is an arithmetic operator unlike [ or [[ which can handle empty strings if quoted properly. An empty value doesn't make sense in an arithmetic context would it? Can you imagine asking "" + 0?
Many beginners seem to think their life will be easier somehow if there are some brackets after if. This is not C, you will be fine, you won't be better off by making things more complicated than they need to be.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.